If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
* The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
is there anyway to access it instead of the number - by id or by value?
Yes, but clumsy:-
Code:
<select name="scattributes" id="attop38" onChange='checkstock(38,39,2155);'>
<option value=''>-First Select Color-</option>
<option value=1993>N:Navy</option>
<option value=1997>BK:Black</option>
<option value=2200>O:Orchid</option>
</select>
<script type = "text/javascript">
var sel = document.getElementById("attop38");
for (var i =0; i<sel.length; i++) {
var val = sel[i].value;
if (val == 1993) {
sel.selectedIndex = i;
}
}
alert ("The selected index is now " + sel.selectedIndex)
</script>
A select list option cannot have an id. Instead of the option value you could use the option text:-
Code:
var val = sel.options[i].text;
if (val == "N:Navy") {
I'm not paranoid! Which of my enemies told you this?
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
function test( setValue )
{
var sel = document.getElementById("attop38");
for ( var i = 0; i < sel.options.length; ++i )
{
if ( sel.options[i].value == setValue )
{
sel.selectedIndex = i;
return; // found it and done
}
}
// no match...do nothing?
}
Quote:
so if i call the function with a 1993 it would select Navy as that is the id of 1993
No. "Navy" is the .text for the <option> with a .value of 1993. There is no id involved in this, except perhaps the id of the <select>. You are using the wrong terminology, which I guess confused the issue.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Last edited by Old Pedant; 03-11-2013 at 07:14 PM..
Options don't have "ID".. text, value.. I think that's it.
__________________ ^_^
If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
* The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
Aside from the fact that you keep repeating the code document.getElementById("attop38") instead of assigning it once to a local variable, how is that different that the code I showed?
Oh...wait...I see...You set the <option>'s selected property to true whereas I changed the selectedIndex. So long as the <select> is not set to muliple, though, those are equivalent.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
At least some older browsers won't support that, LogicAli. (Older versions of FireFox didn't, for example, as I remember finding out the hard way.) Quite possibly (even probably) they are ones he doesn't care about, but...
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Aside from the fact that you keep repeating the code document.getElementById("attop38") instead of assigning it once to a local variable, how is that different that the code I showed?
Oh...wait...I see...You set the <option>'s selected property to true whereas I changed the selectedIndex. So long as the <select> is not set to muliple, though, those are equivalent.
Sorry. Didn't read your code. When I got back to this thread it had a lot of posts that just looked above esthera's knowledge of js so just gave him code. Had I read it I would have said :
Esthera, use Old Pedant's code in post #7.
And yes we both had the same code. How often does that happen?
Actually, seeing the difference between our two is instructive, I think. We were, indeed, almost the same. But knowing that you can either set the <option> to selected or set the selectedIndex to match the desired <option> is good to have in your coding arsenal. (And knowing that you must use selected if you want to set multiple <option>s in a <select multiple> is also good to know. And and and.. The more you know, the better.)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.