getElementByID with array not finding string
-
Hi, The array(0) is foo when I type it out trough DOM so why cant this work? Ive tried a lot, please help! [code] function handleResponse() { if(http.readyState == 4){ var response = http.responseText; var update = new Array(); if(response.indexOf('|' != -1)) { update = response.split('|'); document.getElementById(update[0]).innerHTML = update[1]; //not working! //document.getElementById('foo').innerHTML = update[1];//works! } } } [/code]
-
Hi, The array(0) is foo when I type it out trough DOM so why cant this work? Ive tried a lot, please help! [code] function handleResponse() { if(http.readyState == 4){ var response = http.responseText; var update = new Array(); if(response.indexOf('|' != -1)) { update = response.split('|'); document.getElementById(update[0]).innerHTML = update[1]; //not working! //document.getElementById('foo').innerHTML = update[1];//works! } } } [/code]
-
Why are you creating an array that you don't use?
response.indexOf('|' != -1)
should beresponse.indexOf('|') != -1
--- b { font-weight: normal; }Hi Guffa, Im getting this XmlHttpResonse "foo|This is foo". That was my intention anyway. Turns out I'm getting a lot of other stuff to so that was my problem, array(0).length was 250 not 3 (foo). Btw, seems like there is no difference in those two coding techniques for indexOf, they both work, but yours is more good looking! Thanks! /x
-
Hi Guffa, Im getting this XmlHttpResonse "foo|This is foo". That was my intention anyway. Turns out I'm getting a lot of other stuff to so that was my problem, array(0).length was 250 not 3 (foo). Btw, seems like there is no difference in those two coding techniques for indexOf, they both work, but yours is more good looking! Thanks! /x
xaphod wrote: Btw, seems like there is no difference in those two coding techniques for indexOf, they both work, but yours is more good looking! Are you kidding? My code works, the other doesn't. The expression
'|' != -1
will always evalute to the value true, as the string '|' always will be different from the number -1. As the indexOf method uses a string parameter, the boolean value true will be converted to the string 'true'. The callresponse.indexOf('|' != -1)
therefore does exactly the same asresponse.indexOf('true')
. Unless the string response starts with the letters 'true', the call will always return a non-zero value. --- b { font-weight: normal; } -
xaphod wrote: Btw, seems like there is no difference in those two coding techniques for indexOf, they both work, but yours is more good looking! Are you kidding? My code works, the other doesn't. The expression
'|' != -1
will always evalute to the value true, as the string '|' always will be different from the number -1. As the indexOf method uses a string parameter, the boolean value true will be converted to the string 'true'. The callresponse.indexOf('|' != -1)
therefore does exactly the same asresponse.indexOf('true')
. Unless the string response starts with the letters 'true', the call will always return a non-zero value. --- b { font-weight: normal; }