So I have these checkboxes:
<input type="checkbox" name="type" value="4" /> <input type="checkbox" name="type" value="3" /> <input type="checkbox" name="type" value="1" /> <input type="checkbox" name="type" value="5" />
And so on. There are about 6 of them and are hand-coded (i.e not fetched from a db) so they are likely to remain the same for a while.
My question is how I can get them all in an array (in javascript), so I can use them while making an AJAX $.post
request using Jquery.
Any thoughts?
Edit: I would only want the selected checkboxes to be added to the array
Inside the GetSelected function, first the HTML Table is referenced and then all the CheckBoxes inside it are referenced. Then a loop is executed over the referenced CheckBoxes and inside the loop the value of the selected (checked) CheckBox is inserted into an Array.
Formatted :
$("input:checkbox[name=type]:checked").each(function(){ yourArray.push($(this).val()); });
Hopefully, it will work.
Pure JS
For those who don't want to use jQuery
var array = [] var checkboxes = document.querySelectorAll('input[type=checkbox]:checked') for (var i = 0; i < checkboxes.length; i++) { array.push(checkboxes[i].value) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With