0

I have a checkbox and I marked it as checked, however it doesnt fire the on change function. The note doesn't appear.

My code:
 $('#checkbox1').prop("checked", true);

$('#checkbox1').change(function(){
        if ($(this).is(':checked'))
            $('#s-note').show();
        else
            $('#s-note').hide();
    });
1
  • You expect the onchange to fire without user interaction? Commented Sep 21, 2017 at 20:51

4 Answers 4

2

Do you expect the onchange to fire without user interaction?

Your issue is you set the checked state before you set the handler so if this would have trigged change, you would have not caught it. Your real issue here is setting the property with JavaScript does not fire the change event. So you need to trigger the change event manually.

$('#checkbox1')
  .prop("checked", true)  // set it to checked
  .on("change", function() {  // bind change event
    $('#s-note').toggle($(this).is(':checked')); // toggle visibility
  }).trigger("change"); //trigger that you need the change to run
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, worked perfectly. what about a multi select list? is it gonna be the same?
2

If you are setting the note to be hidden by default and doing it with an id based selector, like this:

#s-note { display:none; }

Then your code won't be able to show it because it also uses an id based selector and that won't be more specific than the selector already in effect.

Instead, you'll have to default the note to hidden using a selector that is less specific than the id selector you will use to show/hide it later. That would be a class.

Also, it's critical that you set up the event handler before you trigger the event, so that when the event happens, there is already an event handler registered.

Now, for your needs, you don't really need the change event, click will do just fine. And, lastly, to ensure that you properly trigger the event, use JQuery's .trigger() method to set things in motion.

// Make sure you set up the callback first
$('#checkbox1').on("click", function(){

        if ($(this).is(":checked"))
            $('#s-note').show();
        else
            $('#s-note').hide();
});

// Then just trigger the event
$('#checkbox1').trigger("click");
.hide { display:none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="checkbox1">Test
<div id="s-note" class="hide">Special</div>

6 Comments

Thank you, but the note is hidden in the start, could you please try that? as it doesnt work for me
@SMH You'll need to show us how you went about hiding the note at the start. If you did it with #s-note { display:none; } it wouldn't work because that is a very specific CSS selector and the JQuery uses the same selector, so it doesn't override it. But, if you are going to programmatically code the checkbox to be checked automatically (and thus show the note), then why bother hiding the note in the first place?
thanks a lot that is very helpful, yes, in case of the list the items display is none, how will i select them from jquery? do you need me to ask another question for this?
@SMH if you give the items classes, rather than id rules, it would work. I'll update the answer to show this.
Thanks, I will post another question to be more specific for the multiselect items
|
0

I hope this code snippet will help you

Approach 1
$('#idcheckbox').on('change', function() {
  if($(this).is(':checked')) {
    alert('checked');
  } else {
   alert('default ');
  }
});

Approach 2
$('input[type=checkbox]').on('change', function() {
if($(this).is(':checked')) {
    alert('checked');
  } else {
   alert('default ');
  }
});

Comments

0

I ran your code in .net mvc and it runs fine.

This is the checkbox same id it hide or shows div element.

<input type="checkbox" id="checkbox1" value=" " />
<div id="s-note" style="display: none">
       <label>Showing</label>
</div>

What are you trying to display?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.