Forum Moderators: open

Message Too Old, No Replies

changing value of button using array as object

         

spykadesign

5:45 pm on Dec 6, 2006 (gmt 0)

10+ Year Member



Here is the snippet from a javascript file im working on.

//get response - works
var result=xmlHttp.responseText;
//split the response into the two parts - works
var splitres=result.split("¦");
// execute another function with the first part - works fune
getPackageInfo(splitres[0]);
// use the second part to change value of button - doesnt work
var x=splitres[1];
document.forms["tmp_pak_react"].elements["tx"+x].value = "";

I keep getting a 'is either null or not object' error. When I change the value of x to say 78; all works fine, but when its 'splitres[1];' it doesnt.

Any ideas? =S

Fotiman

5:49 pm on Dec 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why not try adding some debugging to see what the actual value of x is?

var x=splitres[1];
alert("x = " + x);
document.forms["tmp_pak_react"].elements["tx"+x].value = "";

Also, if your form elements have ids that match "tx"+x, then you could use getElementById instead:

var e = document.getElementById("tx"+x);
if(!e ) alert("Couldn't find element with id: tx" + x);

spykadesign

5:55 pm on Dec 6, 2006 (gmt 0)

10+ Year Member



x is the right value, however it seems it cant find the element with the id tx78, although it does exist..

rocknbil

2:01 am on Dec 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does your document validate? Usually if the object with that ID exists and IS UNIQUE in the document, I've found it's an HTML error or nesting error, like </form></table>. Not that I've ever done that. :-) But it's a common trick. Missing closing tags can do it too.

penders

2:17 pm on Dec 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your 'x' variable is a string that (always?) contains a number... is it possible that your string has some extra white-space before or after the number that is messing up your string concatenation ("tx"+x) ...?

Perhaps convert it to an Int...

var x=parseInt(splitres[1]);

It should be typecast back to a string again when you: "tx"+x ...?