5

I answered this question with this jQuery code:

$('input[type="checkbox"][name$="chkSelect"]').click(function() {
  $('input[type="checkbox"][name$="chkSelect"]').not(this).prop("checked", false);
});

... and it got me thinking: there must be a way to avoid duplicating the selector in the event handler.

I tried $(this).selector but that just returns an empty string. Here's a demo.

Is there a way to get the selector text in the event handler?

Community
  • 1
  • 1
Town
  • 14,706
  • 3
  • 48
  • 72
  • you're `$('____').selector` is only returning __, you can try it with anything like `$('abcdefgi").selector`.. – mazlix Jun 24 '11 at 15:15
  • I get the correct selector (div) in your example. (I'm using FF 5) – Manuel Leuenberger Jun 24 '11 at 15:16
  • @maenu: I think you did not click the element ;) – Felix Kling Jun 24 '11 at 15:17
  • @mazlix: I don't know what you mean - in the first example, `div` is returned. In the second (click) example, empty string is returned. Are you seeing something different? – Town Jun 24 '11 at 15:18
  • @Felix Kling: You're sowhat right ;) – Manuel Leuenberger Jun 24 '11 at 15:19
  • @Town, try `alert($('allyourbasearebelongtous').selector)` and you'll get a popup saying `'allyourbaasearebelongtous'`.. `this`isn't a string so it alerts an empty string. – mazlix Jun 24 '11 at 15:56
  • 1
    @mazlix: That's correct, because in that context `allyourbasearebelongtous` is the selector (although you won't see any elements returned!). `alert` implicitly calls `toString` on the parameter, so `alert(this)` is the same as `alert(this.toString())`, which displays `[object HTMLDivElement]`. `alert($(this).selector)` displays the selector property of a jQuery object created from `this`, but in my demo context it's empty because the underlying element `this` is a DOM element that has no concept of how it was selected. Hope that makes sense! :) – Town Jun 25 '11 at 14:55

4 Answers4

5

$(this).selector does not work because you create a new jQuery object and pass a DOM element, not a selector.

If you only want to avoid repeating the selector, you can cache the elements beforehand (which is better anyway):

var $elements = $('input[type="checkbox"][name$="chkSelect"]');
$elements.click(function() {
    $elements.not(this).prop("checked", false);
});

But I don't think there is a way to get the selector inside the event handler. The only reference you have to the selected elements is the corresponding DOM element (through this). But you cannot "reverse engineer" the selector from that.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • I _knew_ you'd be the one to answer this Felix, cheers :) I figured the first bit out (`this` is the DOM element) but I thought there might be a 'nice' way to get the selector. I guess caching it is the next best thing, plus it means less typing which is always good on a Friday afternoon ;) – Town Jun 24 '11 at 15:19
  • @Town: Exactly :) Happy Friday :) – Felix Kling Jun 24 '11 at 15:20
1

You can always do:

for(var i in arr){
  $(arr[i]).bind('click', {'selector': arr[i]}, function(event){
    console.log(event.data.selector);
  });
}
0
$('div').click(function() {
    alert($(this)[0].nodeName);
});
animuson
  • 53,861
  • 28
  • 137
  • 147
AlexC
  • 9,657
  • 17
  • 64
  • 98
  • That works if you only select elements based on their name. If you have a more complex selector then you are out of luck. Have a lock at the answer @Tom linked to. – Felix Kling Jun 24 '11 at 15:23
  • This is fine as long as the selector directly translates to a node. If you change the selector to eg: `#myDiv` then it fails. – Town Jun 24 '11 at 15:23
0

This guys seems to have done it. How do I get a jQuery selector's expression as text? - see Pim Jager's answer

Community
  • 1
  • 1
Brandon Boone
  • 16,281
  • 4
  • 73
  • 100
  • Cheers Brandon. I did see that, but the line _This is far from optimal but works in some cases_ coupled with the fact it more than a couple of lines long put me off ;) I reckon the caching option is the best. – Town Jun 24 '11 at 15:37
  • I'm also not sure that it's a duplicate - while the essence of the question is the same, the accepted answer in the above link forms part of my original question - so I'd consider it to be more of a progression than a duplicate! :) – Town Jun 24 '11 at 15:41
  • @Town - Ha, I saw that line too after I posted this. Interesting solution, but yeah 'some cases' is not 'enough cases' from my point of view. Glad you were able to find an acceptable solution! – Brandon Boone Jun 24 '11 at 15:55