Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Action JS on drop down select

Posted on 2009-05-12
10
Medium Priority
?
760 Views
Last Modified: 2012-05-06
Morning experts,

Bit of a Javascript question for you..

I have a drop down, that needs to action a specific javascript event when someone clicks on the one the "other" field.

It needs to show a div.

My current code is below. This works absoloutely fine in Firefox 3, however in IE it fails miserably and dosen't action the code.

What can I do?


<select name="event" class="formfield">
	<option selected disabled="disabled">Select your event type</option>
	<option value="1" onClick="javascript:animatedcollapse.hide('type')">Activity Day</option>
	<option value="2" onClick="javascript:animatedcollapse.hide('type')">Conference</option>
	<option value="3" onClick="javascript:animatedcollapse.hide('type')">Corperate Hospitality</option>
	<option value="4" onClick="javascript:animatedcollapse.hide('type')">Dinner</option>
	<option value="5" onClick="javascript:animatedcollapse.hide('type')">Exhibition</option>
	<option value="6" onClick="javascript:animatedcollapse.hide('type')">Gala Dinner</option>
	<option value="7" onClick="javascript:animatedcollapse.hide('type')">Interviews</option>
	<option value="8" onClick="javascript:animatedcollapse.hide('type')">Meeting</option>
	<option value="9" onClick="javascript:animatedcollapse.hide('type')">VIP Meeting</option>
	<option value="10" onClick="javascript:animatedcollapse.hide('type')">Party</option>
	<option value="11" onClick="javascript:animatedcollapse.hide('type')">Product launch</option>
	<option value="12" onClick="javascript:animatedcollapse.hide('type')">Team Building</option>
	<option value="13" onClick="javascript:animatedcollapse.hide('type')">Training Courses</option>
	<option value="14" onClick="javascript:animatedcollapse.toggle('type')">Other</option>
	</select>
 
<div id="type" name="type" style="display:none">
Specify: <input type="text" name="name9"/>
</div>

Open in new window

0
Comment
Question by:billy_howard
[X]
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
  • 5
  • 5
10 Comments
 
LVL 29

Expert Comment

by:Badotz
ID: 24363132
Hard to say without seeing the code for "animatedcollapse.toggle('type')"...

I would attach the onclick event to the <select>, then test the target within the event handler to determine the <option> element that was clicked. Much less typing ;-)
0
 
LVL 1

Author Comment

by:billy_howard
ID: 24363662
badotz, cheers for the reply, it's all about the copy and paste though.... animatedcollapse.toggle is a function from Dynamic drive's code..

http://www.dynamicdrive.com/dynamicindex17/animatedcollapse.htm

Is your link..

Just because I am a JS idiot, what would be the code for working out which form element has been selected and thus activating either the hide or toggle functions of animated collapse?

Cheers
0
 
LVL 29

Accepted Solution

by:
Badotz earned 2000 total points
ID: 24363942
Something along these lines (untested):


function sel_on_click(e) {
    e = e || window.event;
    
    var target = e.target || e.srcElement, // Element that caused the event to fire
        value,
        text;
    
    if (target.nodeType === TEXT_NODE) { // defeat Safari bug 
        target = target.parentNode; 
    }
    // [target] contains the <option> that was clicked
    value = target.value; // [1] through [14]
    text = target.text;   // [Activity Dat] through [Other]
}

Open in new window

0
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

 
LVL 1

Author Comment

by:billy_howard
ID: 24366249
Sorry to be a pain, but I think I am being an idiot..

Have created some code, but I think with my javascript I am thinking far too PHP..


<script type="text/javascript">
function sel_on_click(e) {
    e = e || window.event;
    
    var target = e.target || e.srcElement, // Element that caused the event to fire
        value,
        text;
    
    if (target.nodeType === TEXT_NODE) { // defeat Safari bug 
        target = target.parentNode; 
    }
    // [target] contains the <option> that was clicked
    value = target.value; // [1] through [14]
    text = target.text;   // [Activity Dat] through [Other]
	
	if(value == "14"){
		document.write("Yep, toggled type");
		animatedcollapse.toggle('type');
	} else {
		document.write("Hid type.");
		animatedcollapse.hide('type');
	}
		
}
</script>
</head>
 
<body>
<select name="event" id="event" class="formfield" onChange="javascript:sel_on_click();">
        <option selected disabled="disabled">Select your event type</option>
        <option value="1" onClick="javascript:animatedcollapse.hide('type')">Activity Day</option>
        <option value="2" onClick="javascript:animatedcollapse.hide('type')">Conference</option>
        <option value="3" onClick="javascript:animatedcollapse.hide('type')">Corperate Hospitality</option>
        <option value="4" onClick="javascript:animatedcollapse.hide('type')">Dinner</option>
        <option value="5" onClick="javascript:animatedcollapse.hide('type')">Exhibition</option>
        <option value="6" onClick="javascript:animatedcollapse.hide('type')">Gala Dinner</option>
        <option value="7" onClick="javascript:animatedcollapse.hide('type')">Interviews</option>
        <option value="8" onClick="javascript:animatedcollapse.hide('type')">Meeting</option>
        <option value="9" onClick="javascript:animatedcollapse.hide('type')">VIP Meeting</option>
        <option value="10" onClick="javascript:animatedcollapse.hide('type')">Party</option>
        <option value="11" onClick="javascript:animatedcollapse.hide('type')">Product launch</option>
        <option value="12" onClick="javascript:animatedcollapse.hide('type')">Team Building</option>
        <option value="13" onClick="javascript:animatedcollapse.hide('type')">Training Courses</option>
        <option value="14" onClick="javascript:animatedcollapse.toggle('type')">Other</option>
        </select>
 
<div id="type" name="type" style="display:none">
Specify: <input type="text" name="name9"/>
</div>
 
 
</body>

Open in new window

0
 
LVL 29

Expert Comment

by:Badotz
ID: 24366845
Change:

document.write("Yep, toggled type");
...
...
document.write("Hid type.");

to:

alert('Yep, toggled type');
...
...
alert('Hid type');
0
 
LVL 1

Author Comment

by:billy_howard
ID: 24383500
Hi there,

Got it working but only by commenting out some of your code.

If I comment out the below code it works like a charm..

I don't know enough about JS to be able to debug it. Any ideas?
if (target.nodeType === TEXT_NODE) { // defeat Safari bug 
    target = target.parentNode; 
}

Open in new window

0
 
LVL 29

Expert Comment

by:Badotz
ID: 24386348
Ah, it's a missing constant value. Change:

if (target.nodeType === TEXT_NODE) { // defeat Safari bug

to:

if (target.nodeType === 3) { // defeat Safari bug

Sorry about that...
0
 
LVL 1

Author Comment

by:billy_howard
ID: 24394072
NO problems.

Got it working, thanks VERY much!
0
 
LVL 1

Author Closing Comment

by:billy_howard
ID: 31580466
Perfect. Worked through all the problems, polite quick to reply BRILLIANT!
0
 
LVL 29

Expert Comment

by:Badotz
ID: 24395614
No worries - glad to help.
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.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

636 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