Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  100] [ 1]  / answers: 1 / hits: 20649  / 12 Years ago, fri, july 20, 2012, 12:00:00

I am trying to use a ternary operator to check if the value of an XML element is null. If it is then I want the variable to be one thing. If not then I would like it to return the value of element. This is what I have so far.



var rating = data.getElementsByTagName(overall_average)[0].childeNodes[0].length > 0 ? data.getElementsByTagName(overall_average)[0].childeNodes[0].nodeValue : It is empty;

More From » xml

 Answers
12

The shortest way:



var rating = (data.getElementsByTagName('overall_average')[0].childNodes[0] || {}).nodeValue || 'It is empty';

[#84121] Thursday, July 19, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josefn

Total Points: 251
Total Questions: 93
Total Answers: 84

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;