To select an HTML element by its ID using JavaScript, you can use the getElementById()
function.
Here is an example:
// Select the element with the ID "myElement"
var element = document.getElementById("myElement");
// Manipulate the element using JavaScript
element.style.backgroundColor = "red";
element.innerHTML = "New content";
In this example, the function getElementById()
is called with a string parameter representing the ID of the element you want to select. This returns a reference to the element, which you can then manipulate or work with using JavaScript.