How to get checkbox label values ​​in javascript

I have two checkbox, depending on which user checkboxclicks, I need to call another function, however I am not going to get the label values checkbox.

Here is my code:

function getLabel(id)
{
  return $("#"+id).next().text();
}
function BillStatus(){
    console.log("its here");
    var label=getLabel('checkboxid1'); 
    console.log(label);
    if(label=="No") {
        //some function to call here
    }else{
       //other function to call here
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="check1">
    <label>
        <input type="checkbox" name="myCheckbox" id="checkboxid1" onclick="BillStatus()" style="visibility: visible"/> 
        Yes
    </label>
</div>
<br>
<div id="check2">
    <label>
        <input type="checkbox" id="checkboxid2" name="myCheckbox" value="No" onclick="BillStatus()" style="visibility: visible" />
        No
    </label>
</div>
Run codeHide result
+6
source share
4 answers

There are several typos in the code, but the main problem comes from the return line, you should use parent()instead next():

  return  $("#"+id).parent().text().trim();

NOTE. Use trim()to remove extra spaces from the returned label text.

Hope this helps.

function getLabel(id)
{
  return  $("#"+id).parent().text().trim();
}

function BillStatus(_this){
  console.log("its here");
  
  var label=getLabel(_this.id); 
  
  console.log(label);
  
  if( $(_this).parent().text().trim() === "No"){
    console.log("Nooooo");
  }else{
    console.log("Yessss");
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="check1">
    <label>
        <input type="checkbox" name="myCheckbox" id="checkboxid1" onclick="BillStatus(this)" style="visibility: visible" /> Yes
     </label>
</div>
<br>
<div id="check2">
    <label>
        <input type="checkbox" id="checkboxid2" name="myCheckbox" value="No" onclick="BillStatus(this)" style="visibility: visible" />No
    </label>
</div>
Run codeHide result

jQuery, click , .

$('.checkbox-container input:checkbox').on('click', function()
{
  if( $(this).parent().text().trim() === "No"){
    console.log("Nooooo");
  }else{
    console.log("Yessss");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="check1" class="checkbox-container">
    <label>
        <input type="checkbox" name="myCheckbox" id="checkboxid1"/> Yes
     </label>
</div>
<br>
<div id="check2" class="checkbox-container">
    <label>
        <input type="checkbox" id="checkboxid2" name="myCheckbox" value="No"/>No
    </label>
</div>
Hide result
+3

this BillStatus.

getLabel :

ele.parentElement.textContent.trim();

onchange onclick.

function BillStatus(ele) {
    var label=ele.parentElement.textContent.trim();
    console.log('Label: ' + label + ' Checked: ' + ele.checked);
    if(label=="No") {
        //some function to call here
    }else{
        //other function to call here
    }
}
<div id="check1">
    <label>
        <input type="checkbox" name="myCheckbox" id="checkboxid1" onchange="BillStatus(this)" style="visibility: visible"
                /> Yes</label>
</div>
<br>
<div id="check2">
    <label>
        <input type="checkbox" id="checkboxid2" name="myCheckbox" value="No" onchange="BillStatus(this)" style="visibility: visible" />No</label>
</div>
Hide result

javascript :

document.addEventListener('DOMContentLoaded', function(e) {
   document.querySelectorAll('div[id^=check] [type="checkbox"]').forEach(function(ele, idx) {
       ele.addEventListener('change', function(e) {
           var label = this.parentElement.textContent.trim();
           console.log('Label: ' + label + ' Checked: ' + this.checked);
           if(label=="No") {
               //some function to call here
           }else{
               //other function to call here
           }
       })
   })
})

//
// on document ready
//
document.addEventListener('DOMContentLoaded', function(e) {
    //
    // for each div having aan id starting with and having a checkbox...
    //
    document.querySelectorAll('div[id^=check] [type="checkbox"]').forEach(function(ele, idx) {
        //
        // add the change event handler
        //
        ele.addEventListener('change', function(e) {
            var label = this.parentElement.textContent.trim();
            console.log('Label: ' + label + ' Checked: ' + this.checked);
            if(label=="No") {
                //some function to call here
            }else{
                //other function to call here
            }
        })
    })
})
<div id="check1">
    <label>
        <input type="checkbox" name="myCheckbox" id="checkboxid1" style="visibility: visible"
                /> Yes</label>
</div>
<br>
<div id="check2">
    <label>
        <input type="checkbox" id="checkboxid2" name="myCheckbox" value="No" style="visibility: visible" />No</label>
</div>
Hide result
0

$("input[type='checkbox']:checked:last").next().text()

function getLabel(id)
{
  return $("#"+id).next().text();
}

function BillStatus(){
    console.log("its here");
    var label = $("input[type='checkbox']:checked:last").next().text(); 
    label = $.trim(label);

    if(label== "No") {
        console.log("No is checked");
    }else if(label== "Yes") {
        console.log("Yes is checked");
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="check1">
    <input type="checkbox" name="myCheckbox" id="checkboxid1" onclick="BillStatus()" style="visibility: visible"/>  
    <label>
       Yes
    </label>
</div>
<br>
<div id="check2">
    <input type="checkbox" id="checkboxid2" name="myCheckbox" value="No" onclick="BillStatus()" style="visibility: visible" />
    <label>
        No
    </label>
</div>
Hide result
0

, dom . , .

function getLabel(id)
{
  return $("#"+id).parent().text().trim();
}
function BillStatus(){  
  var elem=this.event.target.id;  
  var label=getLabel(elem); 
  console.log(label);
  if(label=="No") {
alert(label)

}else{
 alert(label)
}
}
<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div id="check1">
  <label>
    <input type="checkbox" name="myCheckbox" id="checkboxid1" onclick="BillStatus()" style="visibility: visible"
           /> Yes</label>
</div>
<br>
<div id="check2">
  <label>
    <input type="checkbox" id="checkboxid2" name="myCheckbox" value="No" onclick="BillStatus()" style="visibility: visible" />No</label>


</div>
</body>
</html>
Hide result
0

All Articles