Document querySelector() Method - Get the first <p> element in the document: - Javascript DOM

Javascript examples for DOM:Document querySelector

Description

Document querySelector() Method - Get the first <p> element in the document:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p>This is a p element.</p>

<p>This is also a p element.</p>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {//from  w ww .ja  va  2 s . co m
    document.querySelector("p").style.backgroundColor = "red";
}
</script>

</body>
</html>

Related Tutorials