1

My script works fine when all of the elements I'm searching for are declared, but I cannot for the life of me figure out how to test if an element exists or not.

$placeHolder.getElementsByTagName(tagname)[0].firstChild.data; // works if the element exists

I've tried testing out these lines of code:

if (!($placeHolder.getElementsByTagName(tagname)[0].firstChild.data in window)) //always true
{   
    alert("this doesn't exist");
    return $noValue;
}
else
    return $placeHolder.getElementsByTagName(tagname)[0].firstChild.data;

Even if the element exists the if statement is true and executes the first block of code, if it doesn't exist it just generates an error saying that '$placeHolder.getElementsByTagName(tagname)[0].firstChild.data' is undefined.

Next I tried this method:

if (typeof $placeHolder.getElementsByTagName(tagname)[0].firstChild.data == "undefined")
    {   
        alert("this doesn't exist");
        return $noValue;
    }
    else
        return $placeHolder.getElementsByTagName(tagname)[0].firstChild.data;

This one actually works correctly if the element does exist, but still yields the same error as the one above it.

I tried making a try catch block, but I ran into the same problem of having to test if the element was undefined. D:<

All I need is a simple way to see if the element in question exists (i.e. if it is or isn't undefined.) Is there a method I'm not aware of or am I doing something wrong?

EDIT2: doing $placeHolder.getElementsByTagName(tagname).length

works if the element isn't defined, but if it is an element like this: <anElement></anElement>

$placeHolder.getElementsByTagName(tagname).length returns true, and it executes the statement when there is nothing in the element. Trying this test:

if ($placeHolder.getElementsByTagName(tagname)[0].firstChild.data == null)

doesn't work either.

1 Answer 1

2

First need to check if there are elements with a specific tagname in your $placeHolder. getElementsByTagName returns NodeList so you can check its length, this way:

if( $placeHolder.getElementsByTagName(tagname).length > 0 )
Sign up to request clarification or add additional context in comments.

5 Comments

YES! It worked! I should have known to check the array's length, another part of my code uses the length attribute...
what would I do if the element is there, but nothing is in it? Like <element></element>
@user2419560 in the similar way: if( $placeHolder.getElementsByTagName(tagname).length > 0 && $placeHolder.getElementsByTagName(tagname)[0].getElementsByTagName(childTagname).length >0 )
I had to change the last condition so it had a ! in front of it, but other than that it worked. Would you mind explaining why it works?
@user2419560 each dom element / xml node has getElementsByTagName method. when you're trying to get node' children of specific type you have to check if there is at least one child with this type (.length > 0). after that you can get node child's children and so on..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.