Javascript: how do i check if node has children

Posted on 2014-03-03
Last Modified: 2014-03-12
I have the following XML and I want a function that will throw an error whenever there's an instance of an Empty <Name> (or where <Name> has no children). How do I capture that?


Open in new window

i have the following code:
var nsPerson = dom.SelectNode(dom.Root, "Person/Name");
if (nsPerson)
	for (var i=0; i < nsPerson.Count; i++)	
		var nPerson =  nsPerson.GetItem(i);
		if (!nPerson)
		  //throw "Empty Person.";

Open in new window

Question by:badtz7229
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 39902678
With document.getElementById("yourItem").parentNode; see
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39902685
Sar1973 how would you update his code using your solution?

Accepted Solution

badtz7229 earned 0 total points
ID: 39913596
i answered my own question.

var nsPerson = dom.SelectNode(dom.Root, "Person/Name");
if (nsPerson)
      for (var i=0; i < nsPerson.Count; i++)      
            var nPerson =  nsPerson.GetItem(i);
            if (nPerson)
                           var hasChildren = nPerson.SelectSingle('*');
                           if (!hasChildren)
                                    throw "Empty, try again.";                  

Author Closing Comment

ID: 39922903
this worked

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Javascript Regex 7 20
selected value 16 24
Hide and Unhide Table 6 24
Make a span connected to the price so when qty changes so does pricing 11 27
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question