Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if logic question 1

Status
Not open for further replies.

Geee

Programmer
Apr 23, 2001
253
GB
I have the xml which is loaded in http.responseXML. The XML can either contain a node called "field" or a node called "error" depending on if the validation was successful. I want to check if there is a node called error so I am using the following code:

Code:
if (http.responseXML.getElementsByTagName('error')[0].getAttribute("value")) {

This works ok but generates a javascript error in the console which states:

http.responseXML.getElementsByTagName("error")[0] has no properties

If the error node doesn't exixst. Is there better IF logic that can avoid this error please?

-Geeeeeeeeeeeeeeeeeeeeeeee-
-=
 
Well.... your if statement is incomplete. It appears that you are checking the existance of a value attribute on the error node. However, since you stated above that the error node may not exist, then you will also need to check for the existance of the error node. You can either nest the conditions, or put them all on one line, but you will need to make sure the condition for the existance of the error node comes first. If it does not exist the condition will evaluate to false and will not bother with evaluating the other conditions. Something like this should work:
Code:
if ([!]http.responseXML.getElementsByTagName('error') && [/!]http.responseXML.getElementsByTagName('error')[0].getAttribute("value")) {

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top