How to Set HTML Variable Values Using JavaScript Inline?

Estimated read time 1 min read

You can set the value of HTML variables using JavaScript inline by using JavaScript event handlers such as onclick and accessing the elements through their id or other attributes. Here’s an example that sets the value of an h1 element with an id of “myTitle” when a button is clicked:

<h1 id="myTitle">Original Title</h1>

<button onclick="document.getElementById('myTitle').innerHTML = 'New Title'">
  Set Title
</button>

In the example above, the onclick event handler is attached to the button and sets the value of the h1 element with an id of “myTitle” when the button is clicked. The getElementById() method is used to access the h1 element and its innerHTML property is set to “New Title”. When the button is clicked, the h1 element updates to display “New Title”.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply