Hello,
I have two sets of lists (state lists) One called state_o and one called state_d and I have a function I do on them
function selectCountry(sel) {
document.getElementById("country_o").selectedIndex = states[sel.value];
}
Now what I want to do is determine if the "sel" is state_o or state_d and change the getElementById("country_o") to either _o or _d depending on what state is selected, so state_o would do country_o and state_d would do country_d
How can I determine the select field name?
Thanks!
-
Keep another parameter in this function to see if its _o or _d and set this value in calling function. then in your javascript method you can call country by -
document.getElementById("country" + param2)Assuming param2 is that additional parameter and it will contain string "_o" or "_d"
-
Use
sel.attr('name')if you're using jQuery orsel.nameif just using plain old JavascriptAssuming you have:
<SELECT name='state_o'> <OPTION value='blah'>BLAH</OPTION> </SELECT> <SELECT name='state_d'> <OPTION value='blah'>BLAH</OPTION> </SELECT>Andy E : *attr()* isn't a valid function on any DOM object.Ravindra Sane : $(sel),attr("name") should do it -
If I've understood your question right, it's just:
var field; if (sel.value == 'state_o') field = 'country_o'; else field = 'country_d'; document.getElementById(field).selectedIndex = states[sel.value]; -
You can access the name property through javascript:
function selectCountry(sel) { var od = sel.name == "state_o" ? "o" : "d"; document.getElementById("country_"+od).selectedIndex = states[sel.value]; }Or if you want to be a bit fancier and keep it on one line, slice the o/d straight from the name string:
function selectCountry(sel) { document.getElementById("country_"+sel.name.slice(-1)).selectedIndex = states[sel.value]; }Jason McCreary : +1 Nice clean function update. -
If
selis the select element,sel.idorsel.nameshould give you what you want.
0 comments:
Post a Comment