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

3rd Jul 2020, 5:37 PM
Priyanshu Gupta(प्रियांशु गुप्ता)
Priyanshu Gupta(प्रियांशु गुप्ता) - avatar
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"); }
3rd Jul 2020, 5:51 PM
Russ
Russ - avatar
+ 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.
4th Jul 2020, 10:16 AM
Russ
Russ - avatar
+ 2
Priyanshu Gupta(प्रियांशु गुप्ता) Seriously? I get the output "There is a p element" on this code.
4th Jul 2020, 8:27 AM
Russ
Russ - avatar
+ 1
Edilson Espinosa could you test your solution in code playground please?
4th Jul 2020, 2:31 AM
Gordon
Gordon - avatar
+ 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
4th Jul 2020, 10:12 AM
Priyanshu Gupta(प्रियांशु गुप्ता)
Priyanshu Gupta(प्रियांशु गुप्ता) - avatar
+ 1
you can check number of p elements in document using this code in script var p = document.querySelectorAll("p") ; console.log(p.length);
5th Jul 2020, 3:10 PM
Divya Mohan
Divya Mohan - avatar
+ 1
ok then replace p in with audio var p = document.querySelectorAll("audio") ; console.log(p.length); if no audio tag console value = 0
5th Jul 2020, 3:18 PM
Divya Mohan
Divya Mohan - avatar
0
Russ not working your code , empty tag is considered as your else case
4th Jul 2020, 2:52 AM
Priyanshu Gupta(प्रियांशु गुप्ता)
Priyanshu Gupta(प्रियांशु गुप्ता) - avatar
0
Edilson Espinosa Hey i don't want to check this
4th Jul 2020, 6:33 AM
Priyanshu Gupta(प्रियांशु गुप्ता)
Priyanshu Gupta(प्रियांशु गुप्ता) - avatar
0
Divya Mohan , Gordon & Russ I just want to check that "audio" element is there in html document or not
5th Jul 2020, 3:15 PM
Priyanshu Gupta(प्रियांशु गुप्ता)
Priyanshu Gupta(प्रियांशु गुप्ता) - avatar