DooDoo
DooDoo

Reputation: 13447

event.srcElement.value equivalent in jQuery

I've got a function that is written in javascript and I want to rewrite that in jQuery This is the function code:

function onclickRbtn(controlID , Message)
     {
         var s = document.getElementById(controlID);
         var sVal = event.srcElement.value;
         for (var i = 0; i < s.rows.length; i++)
          {
             if (s.getElementsByTagName("input")[i].value == sVal)
                 s.getElementsByTagName("label")[i].innerText = Message;
         }
         s.disabled = 'disabled';
    }

What is this code :event.srcElement.value` and what is equivalent in jQuery?

Thanks

Upvotes: 2

Views: 17312

Answers (4)

Zymotik
Zymotik

Reputation: 7307

This works in IE9, IE 8, IE7, Chrome 22, Safari 5.7.1. It does not work in FF 14:

$(event.srcElement || event.target);

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

If you have working JavaScript code, DO NOT rewrite it in jQuery. That's like saying "this hammer can push nails into a wall, but I want to use a sledgehammer instead."

That aside, the code you have is exclusive to IE 8 and below, it won't work in IE 9 or other browsers, because it uses specific terms (global event object, srcElement...)

Here's the code you should use:

function onclickRbtn(controlID,Message,evt) {
    evt = evt || window.event;
    var s = document.getElementById(controlID),
        sVal = (evt.srcElement || evt.target).value,
        inputs = s.getElementsByTagName('input'),
        labels = s.getElementsByTagName('label');

    for( var i=0; i<s.rows.length; i++) {
        if( inputs[i].value == sVal) labels[i].innerText = labels[i].textContent = Message;
    }
    s.disabled = true;
}

And wherever you attach the event, make sure you add the event to the arguments. Examples:

<element onclick="onclickRbtn(controlID, Message, event);">
elem.onclick = function(e) {onclickRbtn(controlID, Message, e);};

Upvotes: 3

Preben Huybrechts
Preben Huybrechts

Reputation: 6111

You mean this:

$("button").click(function(){
    var value = $(this).val();
});

Upvotes: 3

rahul
rahul

Reputation: 187030

You can use event.target

To find out which element is the target of the event you can use

*http://www.quirksmode.org/js/events_properties.html#target*

Upvotes: 4

Related Questions