views:

353

answers:

3

What is the proper syntax for toggling the this.id object

$(this).attr("id").toggle("");

Thanks. Google is surprisingly not helping :(

+3  A: 

toggle() is only used for showing/hiding an element, so your question is not entirely clear.

If you want to remove the id, you could use:

$(this).attr("id","");

Or maybe you want to toggle an element with a specific id:

 $("#myid").toggle();
Philippe Leybaert
I'm pretty sure this is what you want to do, Ankur. You don't need to do all that attr("id","") stuff in order to get a jQuery object that represents the HTML element with the ID you want.
danieltalsky
I will try $("#this.id").toggle(); and $(#this.id).toggle(); - not sure if the "" are required.
Ankur
If Ankur is trying to remove the ID of the element, removeAttr('id') is more appropriate than attr('id', "") - in my opinion any way.
BenTheDesigner
+3  A: 

The reason it is not working is that your first

$(this).attr("id")

Returns a string, the ID of your item. What you probably want is:

$(this).toggle();
Eivind
A: 

This will look for DIVs with any ID attribute and toggle on click:

$('div[id]').click(function() {
   $(this).toggle();
}
David