Jump to content

How to get elements with parent node '<p>'


Recommended Posts

I'm trying to pick out links with the parent '<p>' - my code isn't picking them up. Here's what I have:

if (document.body.getElementsByTagName("a")[ook].parentNode === "p")

I've also tried

if (document.body.getElementsByTagName("a")[ook].parentNode === "<p>")

Does anyone know how you're supposed to do this?

Link to comment
Share on other sites

parentNode returns an object of type HTMLElement. If you want to know the tag name, you can use the nodeName property. The nodeName might be in uppercase, so we need to change it to lower case before testing its value.

if (document.body.getElementsByTagName("a")[ook].parentNode.nodeName.toLowerCase() == "p") {
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...