views:

175

answers:

3

simplified, i've got following code:

 <a id="123" href="#"><span>click</span><span>me</span></a>

when i click on 'click' and 'me' it doesnt seem to work with following jquery selector:

 $("a").click...

maybe its because i click on the children? how could i write the selector so it maps to all children in 'a'?

+5  A: 
Gaby
+1, although I don't think the term "function pointer" really applies in a language where functions are first-class citizens...
Skilldrick
very true.. do not know how to rephrase though, to make it easily understood ..
Gaby
+2  A: 

If you need to get the id of a parent element then you just do this:

$('a').children().click(function() {
  var parentId = $(this).parent('a').attr('id');
  // Code goes here
});
Khanzor
+2  A: 

i have to fetch the parent's id. how could i do this? im currently using event.target.id

$(event.target).closest('a').attr('id') would do it.

But there's no need to use event.target: in an event handler function the special this variable will point to the element you added the handler to, rather than the descendant element that was clicked:

$('a').click(function() {
    alert('id= '+this.id)
});

(Or $(this).attr('id') if you want to force jQuery to do some extraneous work.)

PS. Don't use IDs that start with a number. It's invalid and can confuse some browsers.

bobince