0

I am using multiple check-boxes for some action. My requirement is to get on if the checkbox is checked and off when it is unchecked. But I am getting the value as on if I check/uncheck the checkbox.

<label class="cont">
   <input type="checkbox" name="listingstatus" id="listingstatus<?php echo $doc->module_video_id; ?>" onchange="listingStatus('<?php echo $doc->module_video_id; ?>')">
   <span class="checkmark"></span>
</label>


function listingStatus(module_video_id)
{
  var a = $('#listingstatus'+module_video_id).val();
  console.log(a);
}

4 Answers 4

3

Try is(":checked") with ternary operator:

var a = $('#listingstatus'+module_video_id).is(":checked") ? 'on' : 'off';
Sign up to request clarification or add additional context in comments.

1 Comment

this is the answer
0

Using plain vanilla Javascript you'd go with something like this:

btn.addEventListener('click', () => {
  const checkedInputs = Array.from(document.querySelectorAll('input:checked'));
  const values = [];
  checkedInputs.forEach((checkbox) => {
    values.push(checkbox.value);
  })
  console.log(values);
})
<input type="checkbox" value="foo" />
<input type="checkbox" value="bar" />
<input type="checkbox" value="baz" />
<input type="checkbox" value="wtf" />
<button type="button" id="btn">Console.log checked values</button>

If you need something different, please try to be more clear in what you're asking.

Comments

0

use .checked instead of .value:

 <input type="checkbox" id="chkBox" onchange="foo()">

<script>
function listingStatus()
{
  var isChecked = document.getElementById('chkBox').checked;
  console.log(isChecked);
}
</script>

Comments

0

You can check if checkbox is checked or not by using below script

function listingStatus(module_video_id)
    {
        if ($('#listingstatus' + module_video_id).prop('checked')) {
            console.log('checked do stuff here');
        } else {
            console.log('not checked');
        }
    }

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.