Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't this checkbox, created dynamically with jQuery, be clicked?

jsFiddle

I'm using a jQuery plugin that allows the user to draw boxes in an area. I use jQuery to put a checkbox (along with a dropdown list) in the box that appears when the user lets go of the mouse button (this is towards the bottom of the javascript in the jsFiddle). The problem is, the checkbox is unclickable.

I do have some click checking code in the _mouseStart, _mouseDrag and _mouseStop events to stop another box from being created when you click in an existing box, but I don't think this is causing the problem because the dropdown list that is created can be clicked, and furthermore if you remove the click checking code the checkbox remains unclickable.

What is causing the checkbox to be unclickable? Thanks for reading.

EDIT: Thanks to VinayC's answer, I can now see that the click reaches the checkbox, with this code:

$('#box').click(function(e){
    alert('clicked');
    $(this).attr('checked', true);
});

But the $(this).attr('checked', true); line doesn't make the checkbox checked. Can anyone tell me why? I've updated the jsFiddle

EDIT 2: Harmen noticed that the code assigns the same id to each checkbox. In the actual code there's a counter appended to the id, so each one is unique, but I've taken that out because I think this is just a jQuery issue. I'd change the jsFiddle, but if you just create one box (thus one checkbox), the same problem occurs.

like image 999
ben Avatar asked Sep 28 '10 06:09

ben


People also ask

How can check checkbox is dynamic in jQuery?

Answer: Use the jQuery prop() method You can use the jQuery prop() method to check or uncheck a checkbox dynamically such as on click of button or an hyperlink etc. The prop() method require jQuery 1.6 and above.

Does jQuery work on dynamic content?

With jQuery, what happens is it bind event listeners on each DOM element inline and not on classes or ids. So when the binding method is called, events are bonded on the DOM loaded. To bind event on dynamically loaded DOM, we bind the event to the nearest static parent and give the element we want to bind event.

How can write Click event for dynamically generated button in jQuery?

$(document). ready(function() { $(document). on("click","button . delete-row",function(){ // Do something when button is clicked }); });

How check if checkbox is checked jQuery?

prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.


1 Answers

I've got no idea why, but while fiddling around (yes, on fiddlejs), this seems to do the trick

  $('#box', ui.box).click(
      function(evt){
          evt.stopPropagation();
      }
   );

when setting up the box. See: http://jsfiddle.net/BBh3r/9/

I was actually trying to intercept the event and manually set it checked, but if there's no need to set it then hey.. Perhaps there's an extra event generated somewhere negating the first..? Click's only triggered once though.

Might be related to building jquery checkbox - can't set checked value

PS. Only tested on Chrome for Linux

like image 107
Alexander Sagen Avatar answered Oct 14 '22 20:10

Alexander Sagen