Definition and Usage..
The querySelector() method returns the first element that matches a specified CSS selector(s) in the document.
Note: The querySelector() method only returns the first element that matches the specified selectors. To return all the matches, use the querySelectorAll() method instead.
If the selector matches an ID in document that is used several times (Note that an "id" should be unique within a page and should not be used more than once), it returns the first matching element.
Syntax: document.querySelector(CSS selectors)
CODING:The querySelector() method returns the first element that matches a specified CSS selector(s) in the document.
Note: The querySelector() method only returns the first element that matches the specified selectors. To return all the matches, use the querySelectorAll() method instead.
If the selector matches an ID in document that is used several times (Note that an "id" should be unique within a page and should not be used more than once), it returns the first matching element.
Syntax: document.querySelector(CSS selectors)
<!DOCTYPE html>
<html>
<body>
<h2 class="example">A heading with class="example"</h2>
<h3 class="example">A paragraph with class="example"</h3>
<button onclick="classes()">class name</button>
<p class="example">This is a p element with class="example".</p>
<p>This is a p element.</p>
<button onclick="tag()">Tag</button>
<button onclick="tagclass()">tag with .class name</button>
<button id="id" onclick="chage()">Click ME!</button>
<div><p>I am a p element, my parent is a div element.</p></div>
<div><p>I am a p element, my parent is also a div element.</p></div>
<div><p>I am a p element, my parent is also a div element.</p></div>
<button onclick="divp()">1st Div in p</button>
<h1>A h1 element</h3>
<h4>A h4 element</h2>
<script>
function classes() {
document.querySelector(".example").style.backgroundColor = "red";
}
function tag() {
document.querySelector("p").style.backgroundColor = "red";
}
function tagclass() {
document.querySelector("p.example").style.backgroundColor = "gold";
}
function chage() {
document.querySelector("#id").innerHTML = "Thanks..";
}
function divp() {
var x = document.querySelector("div > p");
x.style.backgroundColor = "orange";
}
document.querySelector("h4, h1").style.backgroundColor = "red";
</script>
</body>
</html>
<html>
<body>
<h2 class="example">A heading with class="example"</h2>
<h3 class="example">A paragraph with class="example"</h3>
<button onclick="classes()">class name</button>
<p class="example">This is a p element with class="example".</p>
<p>This is a p element.</p>
<button onclick="tag()">Tag</button>
<button onclick="tagclass()">tag with .class name</button>
<button id="id" onclick="chage()">Click ME!</button>
<div><p>I am a p element, my parent is a div element.</p></div>
<div><p>I am a p element, my parent is also a div element.</p></div>
<div><p>I am a p element, my parent is also a div element.</p></div>
<button onclick="divp()">1st Div in p</button>
<h1>A h1 element</h3>
<h4>A h4 element</h2>
<script>
function classes() {
document.querySelector(".example").style.backgroundColor = "red";
}
function tag() {
document.querySelector("p").style.backgroundColor = "red";
}
function tagclass() {
document.querySelector("p.example").style.backgroundColor = "gold";
}
function chage() {
document.querySelector("#id").innerHTML = "Thanks..";
}
function divp() {
var x = document.querySelector("div > p");
x.style.backgroundColor = "orange";
}
document.querySelector("h4, h1").style.backgroundColor = "red";
</script>
</body>
</html>
Comments
Post a Comment