tags:

views:

1713

answers:

3

Hi, I have this jQuery which works fine

$("li[id^='shop_id']").click( function () {

  alert("I clicked on id ??");

 });

The above will work if any list element with an id beginging with 'shop_id' is clicked (i.e 'shop_id_1', 'shop_id_2', etc). The problem is that I don't know how to find out exactly which id was clicked. I need to know as I need to set the visability of other elements depending on which id was clicked.

Any ideas ?

Thanks

+7  A: 

The "this" variable will help you out here:

$("li[id^='shop_id']").click( function () {
    var current_id = $(this).attr("id");
    alert(current_id);
});
bchhun
ah, beat me to it.
yaauie
+2  A: 
<script type="text/javascript">
  $("li[id^='shop)id']").click(function(){
    alert($(this).attr('id'));
  });
</script>
yaauie
A: 

Both of you are my saviours. I couldn't find this in the selector documentation, but I guess it's somewhere else in the docs ?

kalium
It's not a selector. A selector allows you to select an object. attr() is a method can operate on a jQuery object.Also, in StackOverflow when you receive a correct answer, it's polite to formally accept the answer. Since Bernard's came first, please accept it -THX, and welcome!
yaauie
take a look at the end of this page : http://docs.jquery.com/Events/click#fn. it's quite subtle but it says that "this" is indeed the current DOM element.
bchhun
Ok. Sorry, hope I didn't offend anyone. I accept Bernard's answer..formally.
kalium
"Unknown", by "formally accept" yaauie meant you should click the big check mark to the left of Bernard's proposed answer so it turns green :)
Danita
Heh, don't I feel silly. Just did it now...
kalium