I want to hide an element after it is first clicked (using jQuery), so that user can't see and click the element after that.
How can I do that?
Thanks.
I want to hide an element after it is first clicked (using jQuery), so that user can't see and click the element after that.
How can I do that?
Thanks.
Very simply:
"selector"
above would be any valid jQuery selector (e.g.".click-to-hide"
to make all elements with classclick-to-hide
have this behavior). Hiding the element is done using the jQueryhide
method (there is alsoshow
if you want to make the elements visible again later).If you do not intend to do anything at all with the elements after they are hidden for the first time, you might also want to consider
remove
instead ofhide
.Update: To do something on the second click, you need to remember when a click has already been made on an element. If it's not going to get more complicated than that, you could use a class for this purpose:
If you want to count the clicks, you can use the
data
function to store the amount of clicks the element has received:I can't comment, but I think the code I use works better than the original answer.
It's just much more simple and compressed.