1

I am developing an application using JQuery. This is a fragment of the HTML code I am using:

<MarkNag class="altcheckboxoff" id="markable_38" Azpimark_id="100038">helburua </MarkNag>    
<MarkNag class="altcheckboxoff" id="markable_2"  Azpimark_id="100002">Oriolek </MarkNag>
<MarkNag class="altcheckboxoff" id="markable_39" Azpimark_id="100039">gas liberalizazioa </MarkNag>

I have the next JQuery script in the HTML page:

<script type='text/javascript'>
   $("MarkNag").click(function (){
      $(this).toggleClass("highlight");
   });
</script>

I would like to know how could I store "markable_39" in a variable if this MarkNag tag was clicked. I guess I should use .data(). But I dont really know how. Any ideas? Thanks

2

5 Answers 5

2

Do it like this

$("MarkNag").click(function () 
 {
       $(this).toggleClass("highlight");

       var IdOfTag = this.id;
       //or
       IdOfTag = $(this).attr('id');  

 });
Sign up to request clarification or add additional context in comments.

Comments

2

Also, you can just use this.id, like:

var id = this.id;

1 Comment

True, and somewhat simpler. Good answer.
1

Actually, the correct code would be $(this).attr("id").

1 Comment

Yeah, I know, just habit to wrap it in $(this). I commented on nymous's answer above.
0
$("MarkNag").click(function (){
   $(this).toggleClass("highlight");

   alert(this.id);               // Method 1: this.id
   alert($(this).attr('id'));    // Method 2: $(this).attr('id')
});

Comments

0

here u will get object from where the event occurs

var eventobject = arguments.callee.caller.arguments[0];

here u can access any attribute of currentTarget (in this case id)

var id = $(eventobject.currentTarget).attr("id");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.