0
D.O.M. element
I want to check that an element say"p" in the html or not . Please tell me how can i do this with js
10 ответов
+ 3
let p_elements = document.getElementsByTagName("p");
if (p_elements.length >= 1) {
console.log("There is a p element");
} else {
console.log("There is not a p element");
}
+ 3
Priyanshu Gupta(प्रियांशु गुप्ता) My apologies, for some reason the code didn't add to my previous message. Here was my code:
https://code.sololearn.com/WS1FACA5S7ka/?ref=app
I get positive output whether tag is empty or not. Maybe you need to put your JS code into a window.onload event trigger.
+ 2
Priyanshu Gupta(प्रियांशु गुप्ता) Seriously? I get the output "There is a p element" on this code.
+ 1
Edilson Espinosa could you test your solution in code playground please?
+ 1
Russ
https://code.sololearn.com/WiHmfxGa5lkc/?ref=app
se this if you have empty tag then it show not p element , and when i add something in p tag , it again show not p element
+ 1
you can check number of p elements in document using this code in script
var p = document.querySelectorAll("p") ;
console.log(p.length);
+ 1
ok then replace p in with audio
var p = document.querySelectorAll("audio") ;
console.log(p.length);
if no audio tag console value = 0
0
Russ not working your code , empty tag is considered as your else case
0
Edilson Espinosa Hey i don't want to check this
0
Divya Mohan , Gordon & Russ I just want to check that "audio" element is there in html document or not