Closed Thread Icon

Preserved Topic: Passing for field to function for SELECT (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18544" title="Pages that link to Preserved Topic: Passing for field to function for SELECT (Page 1 of 1)" rel="nofollow" >Preserved Topic: Passing for field to function for SELECT <span class="small">(Page 1 of 1)</span>\

 
Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-29-2002 16:15

Hmmm... I thought this was discussed here before, but I can't find it.

I want to pass a variable to a function. Using that string, it creates the name of a form field, then changes the value of that form field (a <select> ) to a given value. But my syntax is wrong...

function statechange(mystate){
// alert(document.vcard.mystate);
mycntry = mystate + "country";
if (document.vcard.mystate != ""){
document.vcard.mycntry.options[document.vcard.mycntry.selectedIndex].value = "224";
}
}

Basically, if I call statechange('w), then mycntry should equal 'wcountry', and document.vcard.wcountry.options[document.vcard.wcountry.selectedIndex].value should be set to "224"

I know that the 'wcountry' part is not correct in the syntax, but I don't know what it's called to search for a fix.

Help!

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 07-29-2002 17:02

Change

document.vcard.mycntry...

to

document.vcard[mycntry]...

in both places that it occurs.

There isn't actually an input with name="mycntry" anywhere. What you're doing is taking the *value* of a string and using it as the property of an object. This is called "indirection" and is done with the [] operators. (Though it's more commonly done with eval(), but that's slower and uglier.)

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-29-2002 17:07

Okay, thanks. I had found using something like
document.vcard.elements[mycntry]...

would work as well....I like your version better.

Indirection......hmmmm....I'll learn this stuff yet if it kills me (and sometimes I think it is!)

Thanks again.


Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 07-30-2002 14:48

hmm...

*jots that down*


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

« BackwardsOnwards »

Show Forum Drop Down Menu