views:

119

answers:

4

Hi, i need to have a handler on the calling object of onclick event

ie

<a href="123.com" onclick="click123(event);">link</a>
<script>
function click123(event)
{
//i need <a> so i can manipulate it with Jquery 
}
</script>

I want to do this without the use of $().click or $().live of jquery but with the method described above.

+4  A: 

pass in this in the inline click handler

<a href="123.com" onclick="click123(this);">link</a>

or use event.target in the function (according to the W3C DOM Level 2 Event model)

function click123()
{
    var a = event.target;
}

But of course, IE is different, so the vanilla JavaScript way of handling this is

function doSomething(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
}

or less verbose

function doSomething(e) {

    e = e || window.event;
    var targ = e.target || e.srcElement;
    if (targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug
}

where e is the event object that is passed to the function in browsers other than IE.

If you're using jQuery though, I would strongly encourage unobtrusive JavaScript and use jQuery to bind event handlers to elements.

Russ Cam
thx for the quick response.
Konstantinos
`if (!e) var e = window.event;` is somewhat redundant — `e` is already declared, why do it again? `if` can also be avoided altogether in favor of shorter ternary — `e = e || window.event`. Ditto for `e.target` — those 3 lines are easily replaceable with — `var target = e.target || e.srcElement` ;)
kangax
@kangax - I wouldn't say that `e= e || window.event` is a ternary operator as there are not three arguments but two, it's a `logical-or/default` operator - http://javascript.crockford.com/survey.html. But I take the point, it does look neater and I usually use it. I simply pasted the code from the linked article, but will up my answer now :)
Russ Cam
@Russ Cam Sigh... Next time I'll make sure to drink coffee in the morning before leaving comments :)
kangax
@kangax - No problem, you had a valid point for succinctness :)
Russ Cam
+2  A: 

http://docs.jquery.com/Events/jQuery.Event

Try with event.target

Contains the DOM element that issued the event. This can be the element that registered for the event or a child of it.

Svetlozar Angelov
+1  A: 

The easiest way is to pass this to the click123 function or you can also do something like this(cross-browser):

function click123(e){
  e = e || window.event;
  var src = e.target || e.srcElement;
  //src element is the eventsource
}
jerjer
+1  A: 

The thing with your method is that you clutter your HTML with javascript. If you put your javascript in an external file you can access your HTML unobtrusive and this is much neater.

Lateron you can expand your code with addEventListener/attackEvent(IE) to prevent memory leaks.

This is without jQuery

<a href="123.com" id="elementid">link</a>

window.onload = function () {
  var el = document.getElementById('elementid');
  el.onclick = function (e) {
    var ev = e || window.event;
    // here u can use this or el as the HTML node
  }
}

You say you want to manipulate it with jQuery. So you can use jQuery. Than it is even better to do it like this:

// this is the window.onload startup of your JS as in my previous example. The difference is 
// that you can add multiple onload functions
$(function () {
  $('a#elementid').bind('click', function (e) {
    // "this" points to the <a> element
    // "e" points to the event object
  });
});
Robert Cabri