/    Sign up×
Community /Pin to ProfileBookmark

Getting selectedIndex from more than one select box

I have a form with multiple select boxes. When the user selects from the last select box it trigers an onchange event. How do I trigger the onchange event.

Here are my three select boxes. It doesn’t work this way. Could this be a syntax issue?

[code=html]<label for=’Color_field’>Color</label>:
<select id=”Color” name=”Color”>n<option selected>Please select an item</option>
<option>Color Both Sides</option>
<option>Color Front No Back</option>
<option>Color Front B&W Back</option>

</select><br>
<label for=’Size_field’>Size</label>:
<select id=”Size” name=”Size”>n<option selected>Please select an item</option>
<option>2×3.5</option>
<option>3.35×2.17</option>
<option>Color Front B&W Back</option>
</select><br>
<label for=’Quantity_field’>Quantity</label>:

<select id=”Quantity” name=”Quantity” onchange=”window.location.href=’http://imagews.com/index.php?option=com_virtuemart&page=shop.product_details&flypage=flypage.tpl&product_id=20&Color=’+document.addtocart.Color.options[document.addtocart.Color.selectedIndex].value’&Size=’+document.addtocart.Size.options[document.addtocart.Size.selectedIndex].value’&Quantity)=’+document.addtocart.Quantity.options[document.addtocart.Quantity.selectedIndex].value”><option selected>Please select an item</option>
<option>5000</option>
<option>100</option>
<option>250</option>
<option>500</option>
<option>1000</option>
<option>2500</option>
</select><br>[/code]

It works this way, but in the string the different selectedIndex values do not have a variable for the $_GET super global in php. In the browser url it displays as
Color Both Sides2x3.5500 instead of Color=Color Both Sides&Size=2×3.5&Quantity=500. I have another possible way, but I think there is a sytax problem when I use the equal sign.

Here it is the javascript code.

Doesn’t work.

[code=html]
onchange=”window.location.href=’http://imagews.com/index.php?option=com_virtuemart&page=shop.product_details&flypage=flypage.tpl&product_id=20&’document.addtocart.getElementById(Color )’=’+document.addtocart.Color.options[document.addtocart.Color.selectedIndex]’&’document.addtocart.getElementById(Size )’=’+document.addtocart.Size.options[document.addtocart.Size.selectedIndex].value’&’document.addtocart.getElementById(Quantity )’=’+document.addtocart.Quantity.options[document.addtocart.Quantity.selectedIndex].value”[/code]

Works.

[code=html]<select id=”Quantity” name=”Quantity” onchange=”window.location.href=’http://imagews.com/index.php?option=com_virtuemart&page=shop.product_details&flypage=flypage.tpl&product_id=20&’+document.addtocart.Color.options[document.addtocart.Color.selectedIndex]+document.addtocart.Size.options[document.addtocart.Size.selectedIndex].value+document.addtocart.Quantity.options[document.addtocart.Quantity.selectedIndex].value”><option selected>Please select an item</option>
<option>5000</option>
<option>100</option>
<option>250</option>
<option>500</option>
<option>1000</option>
<option>2500</option>
</select><br>[/code]

Can anyone help? I need to get this done.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 13.2010 — Your option elements have no [B]value[/B]. They have only [B]text[/B]. Should be:
<i>
</i>&lt;option [COLOR="Blue"]value="5000"[/COLOR]&gt;5000&lt;/option&gt;
// and so on
Copy linkTweet thisAlerts:
@padanaramauthorOct 13.2010 — Here is the problem. For some reason when I change the selection in the select box the onchange event does not fire. The javascript is getting the value. I think this is due to syntax.
Copy linkTweet thisAlerts:
@KorOct 14.2010 —  The javascript is getting the value[/QUOTE]
Not really. In IE7 won't work. Other browsers are, indeed, able to take the text of an option as a value, if the value is missing. IE7 (and IE6) will not. Anyway, it will be a good habit [I]to set always the values for the option elements[/I]. Just to keep your code within standards.

back to your problem: I bet you use an XHTML Doctype. If so, keep in mind that for XHTML some special characters are take as XML tokens (< & ), thus you must take care of this. See also:

http://www.microshell.com/programming/php/xml-and-ampersand/

http://articles.techrepublic.com.com/5100-10878_11-5032714.html
×

Success!

Help @padanaram spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...