0

i'm trying to get all the checked items from this form with js. i looked at previous solutions but couldn't find anything that match for me.

<form id="checkform" class="container" style="margin-top:20px;">
    <input type="checkbox" name="MX" value="MX" onclick="func(checkform)"> MX <br>
    <input type="checkbox" name="SPF" value="SPF" onclick="func(checkform)"> SPF <br>
    <input type="checkbox" name="DMARC" value="DMARC" onclick="func(checkform)"> DMARC <br>
    <input type="checkbox" name="VRFY" value="VRFY" disabled> VRFY<br>
    <input type="checkbox" name="SMTPTLS" value="SMTPTLS" disabled> SMTPTLS<br>
    <input type="checkbox" name="OpenRelay" value="OpenRelay" disabled> OpenRelay<br>
    <input type="checkbox" name="ReverseDNS" value="ReverseDNS" disabled> ReverseDNS <br>
    <input type="checkbox" name="BlackList" value="BlackList" disabled> Black List <br>

</form>

im expecting to get a list with all the names of the checked elements.

Keren Levy
  • 11
  • 2

1 Answers1

6

Use find("input:checkbox:checked") to get the check box within the form,hope this helps

$('#checkform').submit(function(e){
e.preventDefault();
var list=[]
$(this).find("input:checkbox:checked").each(function(e){
list.push($(this).val())

})
console.log(list)

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="checkform" class="container" style="margin-top:20px;">
    <input type="checkbox" name="MX" value="MX"  > MX <br>
    <input type="checkbox" name="SPF" value="SPF"  > SPF <br>
    <input type="checkbox" name="DMARC" value="DMARC"  > DMARC <br>
    <input type="checkbox" name="VRFY" value="VRFY" disabled> VRFY<br>
    <input type="checkbox" name="SMTPTLS" value="SMTPTLS" disabled> SMTPTLS<br>
    <input type="checkbox" name="OpenRelay" value="OpenRelay" disabled> OpenRelay<br>
    <input type="checkbox" name="ReverseDNS" value="ReverseDNS" disabled> ReverseDNS <br>
    <input type="checkbox" name="BlackList" value="BlackList" disabled> Black List <br>
<button class="getchecked">Submit</button>
</form>
Deepak A
  • 1,624
  • 1
  • 7
  • 16