Solved

onClick behavior in a drop down menu?

Posted on 2008-10-10
7
941 Views
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:
www.buildmethebest.com/intel_customdesktop.php

<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>
</select>
</div>
</div>

Open in new window

0
Comment
Question by:Doomtomb
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

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

<select name="nvidia" onchange="dostuffhere">
<option>xxxxxxx</option>
<option>xxxxxxx</option>
<option>xxxxxxx</option>
<option>xxxxxxx</option>
</select>
0
 

Author Comment

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

Expert Comment

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

document.getElementById('PriceLabel_8_1');
document.getElementById('PriceLabel_8_2');
document.getElementById('PriceLabel_8_3');
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.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 4

Expert Comment

by:Lordgobbledegook
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);
}
</script>
 
 
<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>
</select>

Open in new window

0
 
LVL 16

Expert Comment

by:sh0e
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.
</explanation>

<solution>
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
</solution>

prices.js:
 // Updates a layer text
189 function SetLayerText(sLayerId, sText) {
190 document.getElementById(sLayerId).innerHTML = sText
191 }
192
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);
201
202 // Update total price
203 CurrentPrice = PriceComponent[CatId][Default[CatId]] - Price;
204 TotalPrice = " <b>[ " + CurrentPrice + " ]</b>";
205 SetLayerText("TotalPriceLabel", TotalPrice);
206
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 }
222
223 // Update Default Item
224 Default[CatId] = ItemId;
225}

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.
0
 
LVL 16

Accepted Solution

by:
sh0e earned 250 total points
ID: 22693478
Sigh, made a big mistake.
Anyhow replace the
   <select id="nvidia" onchange="Hierarchy(8.1);">
   ....
   </select>
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>
					</select>
<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>
 
<script>
function optionFix(elem, CatId){
	for(i=1;i<PriceComponent[CatId].length;i++){
	    myOption = document.getElementById(elem + i);
		myOption.innerHTML = myOption.innerHTML.substring(0, myOption.innerHTML.indexOf('---') + 3) + " " + document.getElementById('PriceLabel_'+CatId+'_'+i).innerHTML;
	}
}
</script>

Open in new window

0
 

Author Closing Comment

by:Doomtomb
ID: 31505267
Excellent! Explanation really helped!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
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.
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

825 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