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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...