[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1009
  • Last Modified:

onClick behavior in a drop down menu?

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
Doomtomb
Asked:
Doomtomb
  • 3
  • 2
  • 2
1 Solution
 
LordgobbledegookCommented:
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
 
DoomtombAuthor Commented:
The only thing is that the onchange="" has a different value depending on what is selected on the drop-down menu.
0
 
sh0eCommented:
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
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!

 
LordgobbledegookCommented:
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
 
sh0eCommented:
<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
 
sh0eCommented:
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
 
DoomtombAuthor Commented:
Excellent! Explanation really helped!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now