Closed Thread Icon

Topic awaiting preservation: Sending form field to js function Pages that link to <a href="https://ozoneasylum.com/backlink?for=8094" title="Pages that link to Topic awaiting preservation: Sending form field to js function" rel="nofollow" >Topic awaiting preservation: Sending form field to js function\

 
Author Thread
Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 02-25-2002 17:40

Greets!
I'm working on the window.open script generator, and I've hit a snag....

I have this function:
function changeValue(myValue,myChange){
if (designWindow.myValue.value == ""){
designWindow.myValue.value = 0;
}
designWindow.myValue.value = parseInt(designWindow.myValue.value) + myChange;
}


And I call it via:

<input type="button" value="+" onclick="changeValue('height',1);" />

So it "should" be setting designWindow.height.value = designWindow.height.value+1

But it's alerting "designWindow.myValue.value is null or not an object". I tried sending the value without the single quotes, but that didn't fix anything. What's up with that? If I hard code the form field name, it works fine, but defeats my purpose for having a function.



mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 02-25-2002 19:02

You're passing string parameter as myValue, and therefore designWindow.myValue.value won't be designWindow.height.value as expected, since it's basically designWindow.'height'.value and that's invalid (string doesn't represent property). You have two options, use eval() to assemble appropriate line of code, or modify your function to look something like this:

function changeValue(myField, myChange)
{
&nbsp;&nbsp;&nbsp;&nbsp;if (myField.value == "")
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myField.value = 0;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;myField.value = parseInt(myField.value) + myChange;
}

And then call it like this:

<input type="button" value="+" onclick="changeValue(this.height,1);" />

BTW <input> button must be located in the same form as height field, since I'm using "this" reserved word to reference parent form...




[This message has been edited by mr.maX (edited 02-25-2002).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 02-25-2002 19:10

One needn't use eval() to make a string represent a property. You only need to use the [] operator, like this:

designWindow[myValue].value

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 02-25-2002 19:19

Doh, I completely forgot about that... It ain't easy knowing syntax of all those language in your head in each moment, heh...

Anyway, Pugzly, if you settle with [] operator, don't forget to add "document." part in front of "designWindow[myValue].value" (i.e. "document.designWindow[myValue].value"), because that's how forms are supposed to be referenced...


Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 02-25-2002 20:46

Bingo...as always, you guys know the answers..

Thanks!

« BackwardsOnwards »

Show Forum Drop Down Menu