Check dynamically created checkbox

Asked

Viewed 542 times

0

Good afternoon.

I need to check if a dynamically created checkbox (is inside a modal) is marked. As it was created dynamically, I thought of the following code:

$(document).ready(function () {
   $(document).on('click', '#elemento', function () {
      if($(document).on("input[id='elemento']:checked")) {
         alert('Sim');
      } else {
         alert('Não');
      }
   });
});

The problem is that this function always returns true. I cannot use this checked. pq it will reference Document, not checkbox. I can’t understand what’s wrong. Someone has some idea?

  • dps of the line code you create this check , you can put $("input").each(function(){ if($(this).is(":checked") == true)console.log("checked")}

  • I happen to be opening the modal by data-target, without writing jquery code.

  • You want to check the checkbox without writing code?

  • No. The code experienced in jquery is in the question. The modal that is opened directly by the id defined in the modal div, in the HTML itself.

  • Yes, but this will refer to Document, not the element. The central point is that the checkbox is created dynamically when the modal is shown. I was able to insert the function in the checkbox click event $(Document). on('click', '#element', Function(), so this part is ok. The problem is in the if($(Document). on("input[id='element']:checked"), which always returns true and I can’t understand the reason. I’m a beginner in jquery.

  • The $(this) will refer to the #elemento , who was informed on .on("click").

  • If I had only $(document).on('click', function () {, then it would be to the document... but you put #elemento as a reference.

  • In fact, it worked. I did what you suggested, it turns out I had tested it before and I hadn’t been able to. I must have forgotten something the first time. Thank you very much.

  • All right. Thanks again.

  • As I put it in the answer, the error was because if was checking if the element exists and if it was not checked. When you put an element inside the if, without making any comparison, for example.: if( $('#elemento') ){ means you want to know if the element exists, already so: if( !$('#elemento') ){ (with "!" exclamation) means you want to know if the element doesn’t exist. I’m glad I helped. If you can mark in the answer to finish, it would be good. Abs!

  • Marquei! I had not scored before pq I’m still familiarizing myself with stackoverflow. Abs

Show 6 more comments

1 answer

1


The problem is that the code $(document).on("input[id='elemento']:checked") will not return the value of checkbox, and yes if the element #elemento exists in the document, and therefore the if will always return true.

To verify the checkbox, swap the if for:

if($(this).is(":checked")) {

Thus remaining:

$(document).ready(function () {
   $(document).on('click', '#elemento', function () {
      if($(this).is(":checked")) {
         alert('Sim');
      } else {
         alert('Não');
      }
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="elemento" checked="checked" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.