0

How to make jquery click function if checkBox checked then status value set to 'true', if not cheked value set to 'false'?

if I check checkbox 1 , then status 1 set to true if I check checkbox 2 , then status 2 set to true if I check checkbox 3 , then status 3 set to true

<div class="form-group">    
  <label><input name="checkBox[]" type="checkbox" value="1"> Checkbox 1</label>
   <input name="status[]" type="text" value="">
</div>
<div class="form-group">    
   <label><input name="checkBox[]" type="checkbox" value="2"> Checkbox 2</label>
   <input name="status[]" type="text" value="">
</div>
<div class="form-group">    
  <label><input name="checkBox[]" type="checkbox" value="3"> Checkbox 3</label>
   <input name="status[]" type="text" value="">
</div>

2
  • 3
    your question is not very clear can you clarify. also include the JS in OP Commented Mar 30, 2017 at 7:22
  • Just bind a change event with checkboxes Commented Mar 30, 2017 at 7:23

4 Answers 4

6

As your input is within <lable>, use parent().next() to set the value.

Here is the working example.

$(function() {
  $('.form-group input[type="checkbox"]').change(function() {
    if ($(this).is(":checked")) {
      $(this).parent().next().val('true')
    } else
      $(this).parent().next().val('false');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
  <label><input name="checkBox[]" type="checkbox" value="1"> Checkbox 1</label>
  <input name="status[]" type="text" value="">
</div>
<div class="form-group">
  <label><input name="checkBox[]" type="checkbox" value="2"> Checkbox 2</label>
  <input name="status[]" type="text" value="">
</div>
<div class="form-group">
  <label><input name="checkBox[]" type="checkbox" value="3"> Checkbox 3</label>
  <input name="status[]" type="text" value="">
</div>

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

Comments

1

Try this :

$('input[type="checkbox"]').change(function() { 
    if($(this).prop('checked')){
       $(this).parent().next().val("true");
    }else{
       $(this).parent().next().val("false");
    }
});

Comments

1

 $('input[type="checkbox"]').change(function () {
            if ($(this).prop('checked')) {  $(this).closest('label').next('input[type="text"]').val('true');
            } else {
                $(this).closest('label').next('input[type="text"]').val('false');
            }
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="form-group">    
  <label><input name="checkBox[]" type="checkbox" value="1"> Checkbox 1</label>
   <input name="status[]" type="text" value="">
</div>
<div class="form-group">    
   <label><input name="checkBox[]" type="checkbox" value="2"> Checkbox 2</label>
   <input name="status[]" type="text" value="">
</div>
<div class="form-group">    
  <label><input name="checkBox[]" type="checkbox" value="3"> Checkbox 3</label>
   <input name="status[]" type="text" value="">
</div>

1 Comment

@silviazulinka Welcome brother
0

You can try this

<div class="form-group">    
   <input name="checkBox" type="checkbox" value="1">
   <input name="status" type="text" value="">
</div>
<div class="form-group">    
   <input name="checkBox" type="checkbox" value="2">
   <input name="status" type="text" value="">
</div>
<div class="form-group">    
   <input name="checkBox" type="checkbox" value="3">
   <input name="status" type="text" value="">
</div>

and the jquery script

$('input[type="checkbox"]').change(function() { 
    if ($(this).is(':checked')) {
        $(this).next('input[type="text"]').val('true');
    } else {
        $(this).next('input[type="text"]').val('false');
    }
}).change();

https://jsfiddle.net/831mjzr1/

2 Comments

Where is the <label> ?
@silviazulinka. Thanks. But this answer is missing the <label>. This answer will fail if <label> is added.

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.