JavaScript Buttons onClick Event getElementById

In this tutorial we are going to use JavaScript Buttons onClick Event getElementById. First thing to know is that we can give any HTML element an id. Here are some examples, we are giving every one of the HTML elements an id of test.

[code]<div id=”test”></div>[/code]

[code]<h2 id=”test”></h2>[/code]

[code]<p id=”test”></p>[/code]

Only cool people share!

[code]<body id=”test”></body>[/code]

We can target any kind of html element that has an d in JavaScript. In the examples above we have 4 different kinds of html elements. Normally you would only put an id on one html element to target it for JavaScript.  I will set up a h3 html element and I will give it a div of change below. Then I will set up a html button element and enter some JavaScript to change whatever is written in the h3 element.

[code]<h3 id=”change”>This is my heading title</h3>[/code]

JavaScript Buttons onClick Event getElementById

Now for the html button that I will use to change the text inside the h3 html element.

[code]<button onClick=”changeHeading()”>Change Heading</button>[/code]

Now to create the JavaScript function that will control the button.

<script>
function changeHeading(){
document.getElementById("change").innerHTML="New Heading Title";
}
</script>

JavaScript Buttons onClick Event getElementById was last modified: January 30th, 2022 by Maximus Mccullough
Summary
JavaScript Buttons onClick Event getElementById
Article Name
JavaScript Buttons onClick Event getElementById
Description
In this tutorial we are going to use JavaScript Buttons onClick Event getElementById to target an HTML element and change the text
Author
JavaScript Buttons onClick Event getElementById

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.