Michael
Michael

Reputation: 37

How to bind back unbinded divs

There is a spot in my code where I need a certain class (which attains to many divs) to be disabled from clicking. So I found

$('.divClass').attr('onclick','').unbind('click');

which works great. Now, after some loading functions, I need to bind all the divs back. Please explain to me why this doesnt work:

 $('.divClass').attr('onclick','').bind('click');

How can I bind the divs back later on.

Upvotes: 0

Views: 38

Answers (1)

Cari
Cari

Reputation: 70

You need to actually assign a function to your bind:

$(".divClass").bind("click", function() {
  // Something happens here
});

If you don't pass the function in you're not making the div do anything if clicked.

Full documentation for bind is here: https://api.jquery.com/bind/

You should note that bind is considered to be obsolete as of jQuery 1.7+ and you should instead use on:

$(".divClass").on("click", function() {
  // Something happens here
});

Full documentation for on is here: https://api.jquery.com/on/

Upvotes: 3

Related Questions