Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-11-2013, 02:05 PM   PM User | #1
esthera
Senior Coder

 
Join Date: May 2004
Posts: 1,430
Thanks: 14
Thanked 0 Times in 0 Posts
esthera can only hope to improve
change option selected

how do I change the selected option

I have

<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></span>
<span id=span39><label>Size</label>
<select name="scattributes" id="attop39" onChange='checkstock(39,,2155);'>
<option value=''>-Next Select Size-</option>
<option value=2151>AXXS:Adult XXS</option>
<option value=2055>AXS:Adult XS</option>
<option value=2010>AS:Adult S</option>
<option value=2008>AM:Adult M</option>
<option value=2006>AL:Adult L</option>
<option value=2004>AXL:Adult XL</option>
</select>


I want in my function to for example say

test("1993")

and this should
set the select box above to have the 1993 option selected
esthera is offline   Reply With Quote
Old 03-11-2013, 03:06 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,365
Thanks: 18
Thanked 348 Times in 347 Posts
sunfighter is on a distinguished road
Code:
<script type="text/javascript">
document.getElementById("attop38").options[1].selected = true;
</script>
Place after the page loads
sunfighter is offline   Reply With Quote
Old 03-11-2013, 03:19 PM   PM User | #3
esthera
Senior Coder

 
Join Date: May 2004
Posts: 1,430
Thanks: 14
Thanked 0 Times in 0 Posts
esthera can only hope to improve
is there anyway to access it instead of the number - by id or by value?
esthera is offline   Reply With Quote
Old 03-11-2013, 03:54 PM   PM User | #4
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
document.getElementById("attop38").options[document.getElementById("attop38").selectedIndex].value;

document.getElementById("attop38").options[document.getElementById("attop38").selectedIndex].text;
__________________
^_^

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".
WolfShade is offline   Reply With Quote
Old 03-11-2013, 03:54 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by esthera View Post
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.

Last edited by Philip M; 03-11-2013 at 04:00 PM..
Philip M is online now   Reply With Quote
Old 03-11-2013, 06:50 PM   PM User | #6
esthera
Senior Coder

 
Join Date: May 2004
Posts: 1,430
Thanks: 14
Thanked 0 Times in 0 Posts
esthera can only hope to improve
I don't care what the current selected index is

all I want to do is change the selected index

so if i call the function with a 1993 it would select Navy as that is the id of 1993
esthera is offline   Reply With Quote
Old 03-11-2013, 07:12 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Code:
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..
Old Pedant is offline   Reply With Quote
Old 03-11-2013, 07:50 PM   PM User | #8
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 03-11-2013, 08:00 PM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by WolfShade View Post
Options don't have "ID".. text, value.. I think that's it.
Yes, I think that has been made clear already. Text, value and selectedIndex.
__________________

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.
Philip M is online now   Reply With Quote
Old 03-11-2013, 11:21 PM   PM User | #10
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,365
Thanks: 18
Thanked 348 Times in 347 Posts
sunfighter is on a distinguished road
Use this esthera:
Code:
<script type="text/javascript">
x = document.getElementById("attop38").options.length;
for(y=0; y<x; y++){
	if(document.getElementById("attop38").options[y].value == '1993')
		document.getElementById("attop38").options[y].selected = true;
}
</script>
sunfighter is offline   Reply With Quote
Old 03-12-2013, 12:17 AM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Sunfighter:

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.
Old Pedant is offline   Reply With Quote
Old 03-12-2013, 12:58 AM   PM User | #12
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Code:
function myFunc( v )
{
  document.getElementById( "attop38" ).value = v;
}
Logic Ali is offline   Reply With Quote
Old 03-12-2013, 01:54 AM   PM User | #13
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 03-12-2013, 03:45 PM   PM User | #14
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,365
Thanks: 18
Thanked 348 Times in 347 Posts
sunfighter is on a distinguished road
Quote:
Originally Posted by Old Pedant View Post
Sunfighter:

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?
sunfighter is offline   Reply With Quote
Old 03-12-2013, 07:42 PM   PM User | #15
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:28 PM.


Advertisement
Log in to turn off these ads.