I need a way by which I can get multiple checkbox selects, I know that there is an onclick event by which i can do it but that is just for single checkbox. Thanks in advvance
HTML
<form action="url" method="post" id="form">
<input type="checkbox" name="box" id="box1" value="checkVal1" />
<input type="checkbox" name="box" id="box2" value="checkVal2" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
JQUERY
$(document).ready(function(){
$("#submit").click(function(){
var value = $("input[type='checkbox']").val();
console.log(value);
})
})
This should work.
That's using jQuery:
$('.select-all').click(function(e) {
e.preventDefault();
CheckAll();
});
function CheckAll() {
$("#checkboxes input").each( function() {
$(this).attr("checked",status);
});
}
HTML:
<p><a href="#" class="select-all">Select All</a></p>
<div id="checkboxes">
<p><input type="checkbox" name="vehicle" value="1" /> Hijo de Satan</p>
<p><input type="checkbox" name="vehicle" value="1" /> Hijo de Satan</p>
<p><input type="checkbox" name="vehicle" value="1" /> Hijo de Satan</p>
<p><input type="checkbox" name="vehicle" value="1" /> Hijo de Satan</p>
<p><input type="checkbox" name="vehicle" value="1" /> Hijo de Satan</p>
</div>
Check this example:
The easiest way its to use a Framework like jQuery and basically you have 3 options, wherever the option you use you can use ":checkbox" as your selected as the sample below, just have in mind that .live will attach the event even when you add new checboxes dynamically if its your case.
.click
$( "#YourCheckBoxID" ).click(function() {
alert( "User clicked on Checkbox" );
});
.bind
$( "#YourCheckboxID" ).bind( "click", function() {
alert( "User clicked on Checkbox" );
});
.live
$(":checkbox").live('click', function() {
alert('User clicked on Checkbox');
});
Regards.
Here is my code which give an array with multiple check box id. You can use the array asper your requirement. Run it in your machine you will get the clear concept
<script type="text/javascript" language="javascript">
var arr = new Array();
function createArr(id){
var chkBoxId = document.getElementById("arr_"+id);
if(chkBoxId.checked){
arr.push(chkBoxId.value);
} else{
for(var i = arr.length - 1; i >= 0; i--) {
if(arr[i] === chkBoxId.value) {
arr.splice(i, 1);
}
}
}
alert(arr); // here you will get your array with multiple checkbox Id and you can use it
}
</script>
<form name="CreateArray" id="CreateArray" method="post">
<?php
for($i=1;$i<=10;$i++){
?>
<?php echo $i.".";?> <input type="checkbox" name="arr[]" value="<?php echo $i?>" id="arr_<?php echo $i?>" onclick="createArr(<?php echo $i?>);"/>
<?php
}
?>
</form>