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”.
+ There are no comments
Add yours