1

My checkBoxes are dynamic and I only need to check condition if my checkbox with "OTHERS" are clicked or not.So My checkboxes are:

<div class="form-group" id="documents">
   <label> <input id="check_id1" type="checkbox" value="1" class="chk1" checked=""> <span>Invoice</span>
   <br>
   </label>
   <div style="padding-bottom: 5px"></div>
   <label> <input id="check_id2" type="checkbox" value="2" class="chk2" checked=""> <span>Packing List</span>
   <br>
   </label>
   <div style="padding-bottom: 5px"></div>
   <label> <input id="check_id3" type="checkbox" value="3" class="chk3" checked=""> <span>OTHERS</span>
   <br>
   </label>
   <div style="padding-bottom: 5px"></div>
</div>

So i tried to check if the "Others" checkboxes is checked or not but alert() is not triggered.

if($("span:contains('OTHERS')").prop('checked') == true){
                alert("here");

            }

5 Answers 5

2

you are not checking prop if input elm instead you are checking prop of span use this code

//gets the input
const elm = $("span:contains('OTHERS')").siblings()[0];
if($(elm).prop('checked')) alert("here");
Sign up to request clarification or add additional context in comments.

2 Comments

is there a way i can put value="3" of OTHERS in a vairable if is is checked?
use $(elm).prop('value'); to access value of input. Then you can set it to a variable under the condition when alert('here') is present
1

It seems, we need to first check if Checkbox is checked or not, and then check if its siblings text value is equal to 'OTHERS'.

I have triggered the alert in this way:

  $("input[type='checkbox']").change(function(){
   if($(this).prop('checked') == true && $(this).siblings("span").text() == "OTHERS"){
        alert("here");
    }
  });

We can also change HTML to simplify it further by directly assigning value="OTHERS" to checkbox.

$("input[value='OTHERS']").change(function(){
     if($(this).prop('checked') == true){
            alert("here");
        }
  });

2 Comments

is there a way i can put value="3" of OTHERS in a vairable if is is checked?
Yes, you can try the code like this : $("input[value='3']")
0

This will look across all checkboxes and check the one that has 'OTHERS'.

document.addEventListener('DOMContentLoaded', () => {
  document.querySelector('#check').addEventListener('click', () => {
    document.querySelectorAll('input[type="checkbox"]').forEach(chk => {
      if(chk.parentNode.querySelector('span').innerText == 'OTHERS') {
        alert(chk.checked);
      }
    });
  });


});
<div class="form-group" id="documents">
   <label> <input id="check_id1" type="checkbox" value="1" class="chk1" checked=""> <span>Invoice</span>
   <br>
   </label>
   <div style="padding-bottom: 5px"></div>
   <label> <input id="check_id2" type="checkbox" value="2" class="chk2" checked=""> <span>Packing List</span>
   <br>
   </label>
   <div style="padding-bottom: 5px"></div>
   <label> <input id="check_id3" type="checkbox" value="3" class="chk3" checked=""> <span>OTHERS</span>
   <br>
   </label>
   <div style="padding-bottom: 5px"></div>
</div>
<!-- Just for the answer -->
<button id="check">Check</button>

Comments

0

This is a DOM traversal issue - you need to go from the span UP to the parent and then DOWN to find the input- that is checked.

Also - note that putting checked="" in the input is the equivalent of checked="checked" - which is why this is showing as checked in the snippet.

EDIT - solution updated to allow the variable to be set based on the click of the label of "OTHERS"

let myvariable;

$('label').click(function(){
  let spanText = $(this).find('span').text();
  let inputChecked = $(this).find('input').is(':checked')

  if(spanText == 'OTHERS' && inputChecked){
     myvariable = 3;
     alert(myvariable);
   }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group" id="documents">
   <label> <input id="check_id1" type="checkbox" value="1" class="chk1"> <span>Invoice</span>
   <br>
   </label>
   <div style="padding-bottom: 5px"></div>
   <label> <input id="check_id2" type="checkbox" value="2" class="chk2"> <span>Packing List</span>
   <br>
   </label>
   <div style="padding-bottom: 5px"></div>
   <label> <input id="check_id3" type="checkbox" value="3" class="chk3"> <span>OTHERS</span>
   <br>
   </label>
   <div style="padding-bottom: 5px"></div>
</div>

2 Comments

is there a way i can put value="3" of OTHERS in a vairable if is is checked?
I just amended my solution to allow the value of 3 to be sotred in a variable if Others is checked.
0

try this

const elm = $("span:contains('OTHERS')").closest("label").find("input[type=checkbox]");
if($(elm).prop('checked')) alert("here");

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.