tags:

views:

756

answers:

7

window.event.srcElement.options(window.event.srcElement.selectedIndex).value only work on IE. How do it work both IE and FF?

A: 
var addEvent = (function() {
function addEventIE(el, ev, fn) {
    return el.attachEvent('on' + ev, function(e) {
 return fn.call(el, e);
    });
}
function addEventW3C(el, ev, fn) {
    return el.addEventListener(ev, fn, false);
}
return window.addEventListener ? addEventW3C:addEventIE;
})();

var domRef = document.getElementById('foo');

addEvent( domRef, 'change', function(e) {
    e = e || window.event;
    var el = e.target ? e.target : e.srcElement,
        value = el.value;
    alert( value )
});

in IE, event is a property of window, in modern DOM supporting browsers it's passed as the first argument.

meder
or in jQuery, use e.target, which is set to srcElement in the case of IE.
mahemoff
Why must every single Javascript question resolve to some jQuery answer/comment?
meder
Could the downvoters at least explain themselves?
meder
A: 

There are two approaches: Assume there is markup "< SELECT name="ddlQuery" id="ddlQuery" style="width:273px;" onchange="GetDropDownValue(event)">...." on HTML

one using js function function GetDropDownValue(e)

    {
        var rtnVal = "";
        var sel = document.getElementById(getTargetID(e));
        for (var i = 0; i < sel.options.length; ++i) {
            if (sel.options[i].selected == true) {
                rtnVal = sel.options[i].value;
                break;
            }
        }
        alert(rtnVal);
        return rtnVal;
    }

another using jQuery: $('#ddlQuery').val()

ig4
+1  A: 

event.target.options[event.target.selectedIndex].value. Though as always with events you'd have to have passed the event object into a function, so eg.:

<script>
    function selectChanged(event) {
        var target= event.target || event.srcElement;
        doSomethingWith(target.options[target.selectedIndex].value);
    };
</script>

<select onchange="selectChanged(event)">...</select>

Setting the handler directly and using this may be easier:

<select id="x">...</select>

<script>
    document.getElementById('x').onchange= function() {
        doSomethingWith(this.options[this.selectedIndex].value);
    };
</script>

Note that looking at options[selectedIndex] is for compatibility with older browsers. These days you can usually just get away with saying select.value.

bobince
A: 

There is no global event object in Firefox. Events are passed to their handlers as an argument. Also, instead of srcElement, you look for target.

If you use a javascript library like jQuery, all the browser specific quirks are handled for you.

Otherwise, I suggest you to read these articles

Chetan Sastry
A: 

IE uses srcElement where most other browsers (including Firefox) use target.

Also, Firefox passes around event objects, whereas IE just populates the global event object w/the current event's data.

You'll have to handle both in your code. How you handle the 2nd one will depend on how you're assigning the handler.

But here's one way.

function changeHanlder( event )
{
  var elem = event.target || event.srcElement;
  alert( elem.options[elem.selectedIndex].value );
}

It's also worth noting that all the modern javascirpt libraries handle this abstraction for you.

Peter Bailey
So where does this "e" come from? I think you meant event.target || event.srcElement.
Maiku Mori
@Maiku - indeed, where is that `e` coming from? Fixed (thanks!)
Peter Bailey
A: 

Sorry I forgot Function getTargetID(e) function getTargetID(e) {

        if (!e) { var e = window.event; }
        var objTarget = e.srcElement ? e.srcElement : e.target;
        return objTarget.id;
    }
ig4
The answers given pretty much account for this, except you don't really need to grab the id, just pass the DOM reference incase the element doesn't have an ID.
meder
A: 

Firefox uses e.htmlEvent.target.nodeName

you can use try/catch to handle both browsers.

Gerard