473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Cross browsers form select element event handler

Hi guys,

I find it frustrating that I need to sniff out IE to make event
handler for the form's select element work across different browsers.
Here's a summary of what I have right now:

var select = /*get the form select element*/
var option = /*get all the option elements*/

if(document.all){

addEvent(select,"change",executeFunction);

}

else{

addEvent(option[i],"click",executeFunction);

}

FYI, I am using John Resig's addevent.

Any advises would be much helpful. Thanks :)

Feb 4 '07 #1
3 2189
al*******@gmail.com said the following on 2/4/2007 12:10 PM:
Hi guys,

I find it frustrating that I need to sniff out IE to make event
handler for the form's select element work across different browsers.
Your code doesn't "sniff out IE", it sniffs out document.all support.
The difference is huge when it comes to Opera and others.

And, the form's select element event handlers work fine, it is the event
handler for OPTION elements you are having problems with.
FYI, I am using John Resig's addevent.
Never heard of it.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 5 '07 #2
On Feb 5, 12:08 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
alvinw...@gmail.com said the following on 2/4/2007 12:10 PM:
Hi guys,
I find it frustrating that I need to sniff outIEto make event
handler for theform'sselectelement work across different browsers.

Your code doesn't "sniff outIE", it sniffs out document.all support.
The difference is huge when it comes to Opera and others.

And, theform'sselectelement event handlers work fine, it is the event
handler for OPTION elements you are having problems with.
FYI, I am using John Resig's addevent.

Never heard of it.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

oh i'm very well aware of the document.all object detection. Sorry for
not being clear enough. The whole point is just to show that I have to
sniff out IE to make it work, not so much as to how I do it or
anything.

Anyway, I think I have found the solution in getting the option value:

var optionValue = this.getAttribute('value') ||
this[this.selectedIndex].value;

Use the first one if you try to get the value by listening to the
onclick event of the 'option' element. The second one is when you try
to get the value by listening to the 'onchange' event of the 'select'
element.

Hope that helps. Thanks.

Feb 5 '07 #3
On Feb 5, 3:10 am, alvinw...@gmail.com wrote:
Hi guys,

I find it frustrating that I need to sniff out IE to make event
handler for the form's select element work across different browsers.
Here's a summary of what I have right now:
You should use feature detection.
var select = /*get the form select element*/
var option = /*get all the option elements*/

if(document.all){
addEvent(select,"change",executeFunction);
}
else{
addEvent(option[i],"click",executeFunction);
}
I don't understand why you are using a different event in different
browsers. Can't you just get the value of the selected option
onchange of the select?

FYI, I am using John Resig's addevent.
I have no idea what that is. Try the following (be careful of IE's
memory leak) which uses a pretty standard addEvent function and makes
IE's this keyword work the same as the W3C event model:

<script type="text/javascript">
function addEvent(el, evType, fn, useCapture) {
if (el.addEventListener) {
el.addEventListener(evType, fn, useCapture);
} else if (el.attachEvent) {
var r = el.attachEvent('on'+evType, function(){fn.call(el);});
} else {
el['on' + evType] = fn;
}
}
function showValue(el){
document.getElementById('d0').innerHTML =
el.options[el.selectedIndex].value;
}
window.onload = function(){
var el = document.forms['f0'].elements['sel0'];
addEvent(el, 'change', function(){showValue(this);}, false);
}
</script>

<form action="" id="f0">
<select name="sel0">
<option value="opt1">opt1
<option value="opt2">opt2
<option value="opt3">opt3
</select>
</form>
<div id="d0"></div>
Incidentally, if you are only adding one handler, it is much simpler
to write

select.onchange = showValue;

And then in the showValue function, 'this' will be a reference to the
select element in both IE and W3C event models.

--
Rob

Feb 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Simba | last post by:
In some pages of my website I use a code like the following: for (var n = 0; n < getTagsArray("SPAN").length; n++){ //SPAN is just an example. I also use other tags tag =...
9
by: Artist | last post by:
Mozilla and IE works different with Javascript. I have Javascript Code which works fine with Mozilla and it breaks with Internet Explorer. I like to convert my code to work with both the...
3
by: Justin Sane | last post by:
I haven't been able to select a form using getElementById. I only can select a form using the "name" property of the <form> tag. Is there another way to select a form, or is it even possible to use...
4
by: John Fereira | last post by:
So, one of the limitations of multipart-form handling is that when an <input type="file" ..> tag is used it will bring up a window which allows a user to select a file for upload but won't allow...
19
by: Taras_96 | last post by:
Hi everyone, How do you detect that a form element has been changed? This thread: ...
4
by: sameergn | last post by:
Hi, I have an image in my HTML form which has onclick() handler. There is also a submit button and a text box. Whenever text box has focus and user presses enter, the onclick() event of...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
1
by: gbezas | last post by:
Hi All, I have added an event handler to redirect form.submit() to a newSubmit() method that I have defined (which does some additional processing before submitting the form). Additionally I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.