Skip to content
the poor coder

run javascript after rendering

Poor Man
Published
Reading time
1 min read

When it comes to running JavaScript after rendering, there are several approaches that can be taken. One option is to use the Document Object Model (DOM) to directly manipulate an HTML element. This can be done by using the getElementById() function to acquire the element from the DOM and then assigning it a value:

Text

const myDiv = document.getElementById('divID');
myDiv.innerHTML = 'Hello World!';

In this example, the element with an ID of 'divID' is accessed and a value of 'Hello World!' is assigned to it. This allows you to dynamically change the contents of the element. Another option is to use a JavaScript library such as jQuery, which makes it much easier to manipulate DOM elements. For example, you can use the jQuery .html() function to directly assign a value to an element:

Text

$('#divID').html('Hello World!');

Once again, this allows you to dynamically change the contents of the element. Finally, you can use JavaScript to add event listeners to elements. This will allow you to trigger a function once a certain event occurs, such as when a user clicks on a button. An example of this would be:

Text

const myDiv = document.getElementById('divID');
myDiv.addEventListener('click', () => {
    myDiv.innerHTML = 'Hello World!';
});

In this example, a click event listener is added to the element with an ID of 'divID'. When the element is clicked, the innerHTML of the element is changed to 'Hello World!'. This allows you to add interactivity to your web page. These are just a few examples of how you can use JavaScript to run code after rendering. Each approach has its own advantages and disadvantages, so it is important to consider which approach best fits your needs.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC