I am new to jQuery. I was trying to get http://jsfiddle.net/heera/bGwrS/30/ to work with my page.
I want my checkbox to function just like this. So if no one selects anything all is selected by default.
My current code is below.
Thanks in advance.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>test</title>
<script type="text/javascript" src="jquery-1.3.2.min"></script>
<script type="text/javascript" language="javascript">
$(function checkbox(fn){
var el=$('input:checkbox[name="city_pref[]"]');
el.on('change', function(e){
if($(this).attr('id')!='city_all')
{
if($(this).is(':checked'))
$('#city_all').prop('checked', false);
else
{
var l=$(':checkbox[name="city_pref[]"]')
.filter(':checked')
.not('#city_all').length;
if(l==0)
$('#city_all').prop('checked', true);
}
}
else
{
if($(this).is(':checked'))
el.not($(this)).prop('checked', false);
}
});
});
</script>
</head>
<body>
<form name="form1" onclick="checkbox(fn)" >
<label for="city_all"><input type="checkbox" name="city_pref[]" id="city_all" value="0" checked />All</label>
<label for="city_pref_1"><input type="checkbox" name="city_pref[]" id="city_pref_1" value="Chicago" />Chicago</label>
<label for="city_pref_2"><input type="checkbox" name="city_pref[]" id="city_pref_2" value="Texas" />Texas</label>
</form>
</body>
</html>

New Topic/Question
Reply


MultiQuote



|