1

I'm running simple JavaScript for my homework.JS is accessing XML table with DOM. When I run this code:

...
var famstatus = xmlDoc.getElementsByTagName('famstatus');
for ( var i = 0 ; i<employee.length; i ++ )
{
document.getElementById("nam").innerHTML += famstatus[i].childNodes[0].textContent + " ";
}
...

I get:

Single Single Married Single Married.... etc.

However when I try to include an IF statement it doesn't achieve the expected results. Here is what I want to do:

...
for ( var i = 0; i < employee.length ; i++ )
{
   if ( famstatus[i].childNodes[0].textContent === "Single" ) 
   {
   alert("found"); 
   }
   else alert("not");
}
...

The script never alerts "found", I get only "not".

3
  • You look like you have 3 "=" signs when comparing in your condition? Commented May 17, 2016 at 15:12
  • Yeah I tried both == and === and neither worked. Commented May 17, 2016 at 15:18
  • add the console.log("famstatus[i].childNodes[0].textContent") and look at console Commented May 17, 2016 at 15:24

1 Answer 1

1

I can't see what document you are working with so it is hard to debug, but there may be other text that isn't apparent so the strings aren't equal. Try replacing

if ( famstatus[i].childNodes[0].textContent === "Single" ) 

with

if ( famstatus[i].childNodes[0].textContent.indexOf("Single") > -1 )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.