onClick behavior in a drop down menu?

Posted on 2008-10-10
Medium Priority
Last Modified: 2012-08-13
How can I apply an onclick behavior to a drop down menu? So that when the user changes a value in the drop-down menu, it performs a javascript.

I have included a sample of the HTML i have currently and my site for reference is:

<div id="Group3">
<div id="Choice8-1">
<input type="radio" name="VideoCard" id="ch8-1" onclick="Reset(3,this.parentNode); Hierarchy(8.1);" Checked>Nvidia
<div id="sub_ch8-1" style="visibility: visible; display: block;">
&nbsp;&nbsp;&nbsp;&nbsp;<select name="nvidia">
<option id="GPU1" onchange="onClickItem(8,1); document.getElementById('image_videocard').src='images/evga_8800gt.jpg'" Selected>GeForce 8800GT  512MB ---<span id='PriceLabel_8_1'></span></option>
<option id="GPU2" onchange="onClickItem(8,2); document.getElementById('image_videocard').src='images/evga_9500gt.jpg'">GeForce 9500GT  512MB ---<span id='PriceLabel_8_2'> <b>[ -50 ]</b></span></option>
<option id="GPU3" onchange="onClickItem(8,3); document.getElementById('image_videocard').src='images/evga_9600gt.jpg'">GeForce 9600GT  512MB ---<span id='PriceLabel_8_3'> [ -15 ]</span></option>

Open in new window

Question by:Doomtomb
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2

Expert Comment

ID: 22693046
Put the onChange part into the <input type> tag, instead of the <option> parts:

<select name="nvidia" onchange="dostuffhere">

Author Comment

ID: 22693080
The only thing is that the onchange="" has a different value depending on what is selected on the drop-down menu.
LVL 16

Expert Comment

ID: 22693083
The onClick and onChange are correct and working.  Your code has mistakes.

All return null, and is causing your script to stop working.

<span> is not allowed inside <option> tag, so browsers just ignore your <span> tag.
You will have to find another way.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 22693108
Easily sorted :)  Set the option values in the form and access them from Javascript.
<script language="javascript">
function DoStuff() {
   myvalue = document.getElementById('nvidia').value;
   alert (myvalue);
<select name="nvidia" id="nvidia" onchange="DoStuff">
   <option value="1">xxxxxxx</option>
   <option value="2">>xxxxxxx</option>
   <option value="3">>xxxxxxx</option>
   <option value="4">>xxxxxxx</option>

Open in new window

LVL 16

Expert Comment

ID: 22693389
<explanation of='dilemma'>
The dependency on the <span>s makes it difficult to modify easily.  This is because the Javascript code uses the CatId and ItemId to generically find the <span> enclosed text it wants to directly change for all items, as well as to calculate and grab values for the items.
So, in order to allow the continued use of the generic Javascript code, without rewriting the author's code for each set of <option>s tags, the Javascript needs to be modified to detect and specially inject for <option> tags, or the <span> has to be emulated somehow.
Otherwise the code would need to be rewritten/refactored.

Emulate span with a hidden div (hidden span has bugs in some browsers like Opera)
 <div id="PriceLabel_8_1" style="display: none"> [-50] </div>

Add to <option> onclick to grab document.getElementById('PriceLabel_8_1') and add on to option text

Code will be posted in next message

 // Updates a layer text
189 function SetLayerText(sLayerId, sText) {
190 document.getElementById(sLayerId).innerHTML = sText
191 }
193 // Function for when a user clicks on the item
194 function onClickItem(CatId,ItemId) {
195 // Price is the price of the default component - the new component
196 Price = PriceComponent[CatId][Default[CatId]] - PriceComponent[CatId][ItemId];
197 // PriceDisplay is the difference, formatted with a + or -
198 PriceDisplay = " <b>[ " + ((Price >= 0)?'+':'') + Price + " ]</b>";
199 // SetLayerText looks for PriceLabel_CatId_Default item and posts the difference
200 SetLayerText("PriceLabel_" + CatId + "_" + Default[CatId], PriceDisplay);
202 // Update total price
203 CurrentPrice = PriceComponent[CatId][Default[CatId]] - Price;
204 TotalPrice = " <b>[ " + CurrentPrice + " ]</b>";
205 SetLayerText("TotalPriceLabel", TotalPrice);
207 // Goes through each PriceComponent starting with 1
208 for (i = 1; i < PriceComponent[CatId].length; i++) {
209 // Price1 is the price of the first component - the new component
210 Price1 = PriceComponent[CatId][i] - PriceComponent[CatId][ItemId];
211 // PriceDisplay1 is the difference, formatted with a + or -
212 PriceDisplay1 = " <b>[ " + (( Price1 >= 0)?'+':'') + Price1 + " ]</b>";
213 // If this is the item that is selected, post nothing
214 if (i == ItemId) {
215 // Remove price info from newly selected item
216 SetLayerText("PriceLabel_" + CatId + "_" + ItemId, "");
217 } else {
218 // Else post the price difference
219 SetLayerText("PriceLabel_" + CatId + "_" + i, PriceDisplay1);
220 }
221 }
223 // Update Default Item
224 Default[CatId] = ItemId;

This one was a serious bugger to do.  I better get some love for this one.
By the way, why did you change the id to "nvidia"?  That part was working fine before.  Please do not modify any more code before trying my solution.
LVL 16

Accepted Solution

sh0e earned 1000 total points
ID: 22693478
Sigh, made a big mistake.
Anyhow replace the
   <select id="nvidia" onchange="Hierarchy(8.1);">
section with the code below.  It will work for now.  A cleaned up fix should be patched into prices.js.
<select id="8.1" onchange="Hierarchy(8.1);">
					<option id="GPU1" value="8800GT" onclick="onClickItem(8,1); document.getElementById('image_videocard').src='images/evga_8800gt.jpg'; optionFix('GPU', 8, 1);" Selected>GeForce 8800GT  512MB ---</option>
					<option id="GPU2" value="9500GT" onclick="onClickItem(8,2); document.getElementById('image_videocard').src='images/evga_9500gt.jpg'; optionFix('GPU', 8, 2);">GeForce 9500GT  512MB --- </option>
					<option id="GPU3" value="9600GT" onclick="onClickItem(8,3); document.getElementById('image_videocard').src='images/evga_9600gt.jpg'; optionFix('GPU', 8, 3);">GeForce 9600GT  512MB --- [ -15 ]</option>
<span id='PriceLabel_8_1' style='display:none'></span>
<span id='PriceLabel_8_2' style='display:none'> <b>[ -50 ]</b></span>
<span id='PriceLabel_8_3' style='display:none'> [ -15 ]</span>
function optionFix(elem, CatId){
	    myOption = document.getElementById(elem + i);
		myOption.innerHTML = myOption.innerHTML.substring(0, myOption.innerHTML.indexOf('---') + 3) + " " + document.getElementById('PriceLabel_'+CatId+'_'+i).innerHTML;

Open in new window


Author Closing Comment

ID: 31505267
Excellent! Explanation really helped!

Featured Post

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question