Click to See Complete Forum and Search --> : need help: passing object to a function


kari
05-14-2003, 12:47 PM
Hi,

I want to pass an object(name/value) to java script function

<select name=s1 onChange=populateNames(s1)>
where s1 is object (name)

instead of this ...

function populateNames1() {

var s1val = eval ('window.document.forms[0].s1')
var s1value = s1val.value
alert (s1.value)
................
}

function populateNames2() {

var s2val = eval ('window.document.forms[0].s2')
var s2value = s2val.value
alert (s2.value)
................
}


I want to do it genaric way passing object instead of writing function for each object

How can I pass objects here ?

Please help.

thanks
kari

Jona
05-14-2003, 12:55 PM
function populateNames(s){
var s1value = eval("document.forms[0]."+s)
var s1value = s1value.value;
alert (s1.value);
}

But I would do this:

function populateNames(s){
alert(s.value);
}

Also, you should change your form from:

<select name=s1 onChange=populateNames(s1)>

To this:

<select name="s1" onChange="populateNames(this)">