0

I have this multiple checkboxes with same name attribute : myTask

<input type="checkbox" class="sol-checkbox" name="myTask" value="1B">
<input type="checkbox" class="sol-checkbox" name="myTask" value="2B">
<input type="checkbox" class="sol-checkbox" name="myTask" value="3B">

and

I have this jQuery code to perform every time the user clicks on the checkboxes

$("input[name='myTask'").change(function(){
   alert(1);
});

is there anything wrong with my script?

5
  • 1
    $("input[name=myTask]").change(function(){ I tried this but still not working.. Commented Nov 5, 2019 at 2:59
  • Ok after $("input[name=myTask]") you may need to wrap your code in $(document).ready(function(){ // your code here }) .. ِAlso keep eyes on console for errors Commented Nov 5, 2019 at 3:02
  • $(document).ready(function(){ $("input[name=myTask]").change(function(){ alert(1); }); }); Tried this one still not working and I also checked the console log no error found Commented Nov 5, 2019 at 3:06
  • 1
    You can try $(document).on('change' , "input[name=myTask]" , function(){alert(1)}) Commented Nov 5, 2019 at 3:08
  • Please hit f12 and note any console errors. If you wrap it in a document.load you shouldn't get any issues (unless it wants you to put your script below your body - which happens. Please check the console. Commented Nov 5, 2019 at 3:29

2 Answers 2

1

Please check may be something wrong in your code. You have forgot to add one square bracket

$("input[name='myTask']").change(function(){
   alert($(this).val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="sol-checkbox" name="myTask" value="1B">
<input type="checkbox" class="sol-checkbox" name="myTask" value="2B">
<input type="checkbox" class="sol-checkbox" name="myTask" value="3B">

Sign up to request clarification or add additional context in comments.

Comments

1

You forgot add one closing square bracket .

Please try below code.

Way to trigger the on change through the input type.

// Shorthand for $( document ).ready()
$(function() {
    console.log( "ready!" );
    $("input[name='myTask']").change(function(){
        alert($(this).val());
    });
});

Way to trigger the on change through the class name of the input field.

// Shorthand for $( document ).ready()
$(function() {
    console.log( "ready!" );
    $(".sol-checkbox").change(function(){
        alert($(this).val());
    });
});

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.