onClick behavior in a drop down menu?

Posted on 2008-10-10
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
  • 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.
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.


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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Angular- another way of coding hide and display 3 40
initialise the object angular2 4 34
Why my select dropdown does not work? 8 27
how can i select 4 7
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now