I have problem regarding on how i will limit my default checked checkbox. for example i have 7 checkboxes. and i want to limit it with 3 default checked checkboxes once the page is load.
this should be the output:
Checkbox1 : true
Checkbox2 : true
Checkbox3 : true
Checkbox4 : false
Checkbox5 : false
Checkbox6 : false
Checkbox7 : false
Here's my sample code:
var mvp = 3;
$(document).ready(function() {
$("input:checkbox").each(function( index ) {
($this).attr("checked",true);
});
});
I'm stock with this, i don't know where i will put my counter (mvp) inside my each function. in this code, all my checkboxes are checked :D.
Sorry for a newbie question, please help me..
Based of @Kevin Bowersox's code:
i modified this code into this:
demo
Using jquery
:lt
selector,it will match all element which has a smaller index.Example:
There is actual no need for a counter since the
each
function will pass in the index. Just use the index as the counter.JS Fiddle: http://jsfiddle.net/SB6aD/2/
Don't setup the default state of your page with Javascript, use the HTML "checked" attribute.
In this example the car checkbox will be checked.
Check .slice
See the JS Fiddle
And note,
checked
is rather a property than an attribute, use.prop
instead of.attr
.