Here i want to do while onclick the button i want to uncheck the fields,and i am using data list , in that datalist suppose i selected Marathahalli means except marathahalli remaining all values i want to make uncheck, i tried but here all values are unchecking, how to do if anyone knows means please update my snippet
$('.options').on('change', function() {
var locality = $('#options option[value="' + $('#areaName').val() + '"]').data('id');
$('#locality_id').val(locality);
}).change();
$("#search1").click(function(){
var localityId = $("#locality_id").val();
if(localityId != ''){
$('.myCheckbox').attr('checked', false); // Unchecks it
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input list="options" type="name" class="form-control options" style="height: 34px; width: 100%;border-radius:0px;" name="areaName" id="areaName" placeholder="Locality" rel="" value="">
<datalist id="options">
<option data-id="1" value="Marathahalli"></option>
<option data-id="2" value="Silk Board"></option>
<option data-id="3" value="Jaya Nagar"></option>
<option data-id="5" value="Kadubeesanahalli"></option>
<option data-id="6" value="Bellandur"></option>
<option data-id="7" value="Kundalahalli"></option>
<option data-id="8" value="JP nagar"></option>
</datalist>
<input type="hidden" name="localityId" id="locality_id" value="">
<button id="search1" type="button">Click Me!</button>
<form id="filterForm">
<ul style="padding-left: 0px;">
<li><label class="space"><input type="checkbox" name="localityId" value="1" checked="" onclick="myFunction()" class="myCheckbox">Marathahalli</label></li>
<li><label class="space"><input type="checkbox" name="localityId" value="2" checked="" onclick="myFunction()" class="myCheckbox">Silk Board</label></li>
<li><label class="space"><input type="checkbox" name="localityId" value="3" checked="" onclick="myFunction()" class="myCheckbox">Jaya Nagar</label></li>
<li><label class="space"><input type="checkbox" name="localityId" value="5" checked="" onclick="myFunction()" class="myCheckbox">Kadubeesanahalli</label></li>
<li><label class="space"><input type="checkbox" name="localityId" value="6" checked="" onclick="myFunction()" class="myCheckbox">Bellandur</label></li>
<li><label class="space"><input type="checkbox" name="localityId" value="7" checked="" onclick="myFunction()" class="myCheckbox">Kundalahalli</label></li>
<li><label class="space"><input type="checkbox" name="localityId" value="8" checked="" onclick="myFunction()" class="myCheckbox">JP nagar</label></li>
</ul>
</form>
This is happening because you are explicitly marking all of them as unchecked and not filtering them. Modified you code.
$('.options').on('change', function() {
var locality = $('#options option[value="' + $('#areaName').val() + '"]').data('id');
$('#locality_id').val(locality);
}).change();
$("#search1").click(function(){
var localityId = $("#locality_id").val();
if(localityId != ''){
$('.myCheckbox').each(function () {
if (localityId === $(this).val()) {
$(this).attr('checked', true);
} else {
$(this).attr('checked', false);
}
})
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input list="options" type="name" class="form-control options" style="height: 34px; width: 100%;border-radius:0px;" name="areaName" id="areaName" placeholder="Locality" rel="" value="">
<datalist id="options">
<option data-id="1" value="Marathahalli"></option>
<option data-id="2" value="Silk Board"></option>
<option data-id="3" value="Jaya Nagar"></option>
<option data-id="5" value="Kadubeesanahalli"></option>
<option data-id="6" value="Bellandur"></option>
<option data-id="7" value="Kundalahalli"></option>
<option data-id="8" value="JP nagar"></option>
</datalist>
<input type="hidden" name="localityId" id="locality_id" value="">
<button id="search1" type="button">Click Me!</button>
<form id="filterForm">
<ul style="padding-left: 0px;">
<li><label class="space"><input type="checkbox" name="localityId" value="1" checked="" onclick="myFunction()" class="myCheckbox">Marathahalli</label></li>
<li><label class="space"><input type="checkbox" name="localityId" value="2" checked="" onclick="myFunction()" class="myCheckbox">Silk Board</label></li>
<li><label class="space"><input type="checkbox" name="localityId" value="3" checked="" onclick="myFunction()" class="myCheckbox">Jaya Nagar</label></li>
<li><label class="space"><input type="checkbox" name="localityId" value="5" checked="" onclick="myFunction()" class="myCheckbox">Kadubeesanahalli</label></li>
<li><label class="space"><input type="checkbox" name="localityId" value="6" checked="" onclick="myFunction()" class="myCheckbox">Bellandur</label></li>
<li><label class="space"><input type="checkbox" name="localityId" value="7" checked="" onclick="myFunction()" class="myCheckbox">Kundalahalli</label></li>
<li><label class="space"><input type="checkbox" name="localityId" value="8" checked="" onclick="myFunction()" class="myCheckbox">JP nagar</label></li>
</ul>
</form>
Related
This question already has answers here:
Check/Uncheck checkbox with JavaScript
(12 answers)
Closed 5 years ago.
Here i am having two fields
Property Type
Bedroom
Suppose i am i am clicking Property Type is Plot/Site/Land i want to make uncheck for Bedroom, how can do this?
Property Type :
<fieldset class="checkboxes">
<label><input type="checkbox" name="propertyType" value="4" onclick="myFunction()"> Apartment</label>
<label><input type="checkbox" name="propertyType" value="5" onclick="myFunction()"> Independent House/ Villa</label>
<label><input type="checkbox" name="propertyType" value="6" onclick="myFunction()"> Individual House/ Standalone Building</label>
<label><input type="checkbox" name="propertyType" value="7" onclick="myFunction()"> Plot/Site/Land</label>
</fieldset>
Bedroom:
<ul style="padding-left: 0px;">
<li class="col-md-12" style="padding-left: 0px;"><label class="space"><input value="1 Room/Hall" name="unitType" type="checkbox" onclick="myFunction()">1 Room/Hall</label></li>
<li class="col-md-12" style="padding-left: 0px;"><label class="space"><input value="1 RK" name="unitType" type="checkbox" onclick="myFunction()">1 RK</label></li>
<li class="col-md-12" style="padding-left: 0px;"><label class="space"><input value="1 BHK" name="unitType" type="checkbox" onclick="myFunction()">1 BHK</label></li>
<li class="col-md-12" style="padding-left: 0px;"><label class="space"><input value="2 BHK" name="unitType" type="checkbox" onclick="myFunction()">2 BHK</label></li>
<li class="col-md-12" style="padding-left: 0px;"><label class="space"><input value="3 BHK" name="unitType" type="checkbox" onclick="myFunction()">3 BHK</label></li>
<li class="col-md-12" style="padding-left: 0px;"><label class="space"><input value="4 BHK" name="unitType" type="checkbox" onclick="myFunction()">4 BHK</label></li>
<li class="col-md-12" style="padding-left: 0px;"><label class="space"><input value="4+ BHK" name="unitType" type="checkbox" onclick="myFunction()">4+ BHK</label></li>
</ul>
Register a change() listener on your property type checkbox and uncheck all the unit types.
$(function() {
$("input[name=propertyType]").change(function() {
$("input[name=unitType]").prop("checked", false);
});
});
function myFunction() {
console.log('myfunction called');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Property Type :
<fieldset class="checkboxes">
<label><input type="checkbox" name="propertyType" value="4" onclick="myFunction()"> Apartment</label>
<label><input type="checkbox" name="propertyType" value="5" onclick="myFunction()"> Independent House/ Villa</label>
<label><input type="checkbox" name="propertyType" value="6" onclick="myFunction()"> Individual House/ Standalone Building</label>
<label><input type="checkbox" name="propertyType" value="7" onclick="myFunction()"> Plot/Site/Land</label>
</fieldset>
Bedroom:
<ul style="padding-left: 0px;">
<li class="col-md-12" style="padding-left: 0px;"><label class="space"><input value="1 Room/Hall" name="unitType" type="checkbox" onclick="myFunction()">1 Room/Hall</label></li>
<li class="col-md-12" style="padding-left: 0px;"><label class="space"><input value="1 RK" name="unitType" type="checkbox" onclick="myFunction()">1 RK</label></li>
<li class="col-md-12" style="padding-left: 0px;"><label class="space"><input value="1 BHK" name="unitType" type="checkbox" onclick="myFunction()">1 BHK</label></li>
<li class="col-md-12" style="padding-left: 0px;"><label class="space"><input value="2 BHK" name="unitType" type="checkbox" onclick="myFunction()">2 BHK</label></li>
<li class="col-md-12" style="padding-left: 0px;"><label class="space"><input value="3 BHK" name="unitType" type="checkbox" onclick="myFunction()">3 BHK</label></li>
<li class="col-md-12" style="padding-left: 0px;"><label class="space"><input value="4 BHK" name="unitType" type="checkbox" onclick="myFunction()">4 BHK</label></li>
<li class="col-md-12" style="padding-left: 0px;"><label class="space"><input value="4+ BHK" name="unitType" type="checkbox" onclick="myFunction()">4+ BHK</label></li>
</ul>
I have this script. Everything is working except I need to make it work with multi-able items. The first set of work perfectly, but I can seam to replicate it on the second and third set of check boxes.
$(".checkall").on('change', function()
{
// all normal checkboxes will have a class "chk_xxxxxx"
// where "xxxx" is the name of the location, e.g. "chk_wales"
// get the class name from the id of the "check all" box
var checkboxesClass = '.chk_' + $(this).attr("id");
// now get all boxes to check
var boxesToCheck = $(checkboxesClass);
// check all the boxes
boxesToCheck.prop('checked', this.checked);
});
$("#check1 input[type=checkbox], #wales").on("change", function()
{
checkBoxes();
});
function checkBoxes()
{
$("#checked").empty();
if($("#check1 input[type=checkbox]:checked").length == $("#check1 input[type=checkbox]").length)
{
$("#wales").prop("checked", true);
// Display the id of the "check all" box
$("#checked").html($("#checked").html() + "<h3>Wales</h3>");
}
else
{
$("#wales").prop("checked", false);
$("#check1 input[type=checkbox]:checked").each(function()
{
$("#checked").html($("#checked").html() + $(this).next().text() + "<br>");
});
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- <input type="checkbox" id="wales" class="checkall" value="1">
<label for="wales" >Check All</label>
<input type="checkbox" id="checkItem1" value="2" class="chk_wales">
<label for="checkItem1" >Item 1</label>
<input type="checkbox" id="checkItem2" value="2" class="chk_wales">
<label for="checkItem2" >Item 2</label>
<input type="checkbox" id="checkItem3" value="2" class="chk_wales">
<label for="checkItem3" >Item 3</label>-->
<hr />
<input type="checkbox" id="wales" class="checkall" value="1">
<label for="wales">Check All</label>
<section id="check1">
<input type="checkbox" id="checkItem1" value="2" class="chk_wales">
<label for="checkItem1">Item 1</label>
<input type="checkbox" id="checkItem2" value="2" class="chk_wales">
<label for="checkItem2">Item 2</label>
<input type="checkbox" id="checkItem3" value="2" class="chk_wales">
<label for="checkItem3">Item 3</label>
</section>
<hr />
<input type="checkbox" id="west" class="checkall" value="3">
<label for="west">Check All</label>
<section id="check2">
<input type="checkbox" id="checkItem4" value="4" class="chk_west">
<label for="checkItem4">Item 1</label>
<input type="checkbox" id="checkItem5" value="4" class="chk_west">
<label for="checkItem5">Item 2</label>
<input type="checkbox" id="checkItem6" value="4" class="chk_west">
<label for="checkItem6">Item 3</label>
</section>
<hr />
<input type="checkbox" id="east" class="checkall" value="5">
<label for="east">Check All</label>
<section id="check3">
<input type="checkbox" id="checkItem7" value="6" class="chk_east">
<label for="checkItem7">Item 1</label>
<input type="checkbox" id="checkItem8" value="6" class="chk_east">
<label for="checkItem8">Item 2</label>
<input type="checkbox" id="checkItem9" value="6" class="chk_east">
<label for="checkItem9">Item 3</label>
</section>
<p>You have selected:</p>
<div id="checked">
</div>
</body>
</html>
Please can anyone help?
Many thanks in advance.
remove checkBoxes() method and replace the last event handlers as (check this fiddle)
//put the event handler directly on the checkboxes inside the section
$( "section input[type='checkbox']" ).on("change", function()
{
console.log( $( this ) );
//get the handle to the parent section
var $parentSection = $( this ).parent();
//compare the number of checked checkboxes with total one
if( $parentSection.find( "input[type='checkbox']:checked" ).length == $parentSection.find( "input[type=checkbox]" ).length )
{
$parentSection.prev().prev().prop("checked", true);
//write checkall's textbox id
$("#checked").append( "<h3>" + $parentSection.prev().prev().attr( "id" ) + "</h3>");
}
else
{
$parentSection.prev().prev().prop("checked", false);
//write each and every checked box's text
$parentSection.find("input[type='checkbox']:checked").each(function()
{
$("#checked").html($("#checked").html() + $(this).next().text() + "<br>");
});
}
});
Following code snippet should fulfill your requirements. Hope this will help you.
var checkedDiv = $("#checked");
$('input[type=checkbox]').on('change', function() {
if (this.className == 'checkall') {
var section = $(this).nextAll('section:first');
section.find('input[type=checkbox]').prop('checked', this.checked);
} else {
var parentSection = $(this).parent();
var checkall = parentSection.prevAll(".checkall:first")
var isEqual = parentSection.find("input[type=checkbox]:checked").length == parentSection.find("input[type=checkbox]").length;
checkall.prop("checked", isEqual);
}
checkedDiv.html('');
$('.checkall').each(function() {
if (this.checked) {
checkedDiv.append('<h3>' + this.id + '</h3><br/>');
} else {
var section = $(this).nextAll('section:first');
section.find("input[type=checkbox]:checked").each(function() {
checkedDiv.append($(this).next().text() + "<br>");
});
}
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="wales" class="checkall" value="1">
<label for="wales">Check All</label>
<section id="check1">
<input type="checkbox" id="checkItem1" value="2" class="chk_wales">
<label for="checkItem1">Item 1</label>
<input type="checkbox" id="checkItem2" value="2" class="chk_wales">
<label for="checkItem2">Item 2</label>
<input type="checkbox" id="checkItem3" value="2" class="chk_wales">
<label for="checkItem3">Item 3</label>
</section>
<hr />
<input type="checkbox" id="west" class="checkall" value="3">
<label for="west">Check All</label>
<section id="check2">
<input type="checkbox" id="checkItem4" value="4" class="chk_west">
<label for="checkItem4">Item 1</label>
<input type="checkbox" id="checkItem5" value="4" class="chk_west">
<label for="checkItem5">Item 2</label>
<input type="checkbox" id="checkItem6" value="4" class="chk_west">
<label for="checkItem6">Item 3</label>
</section>
<hr />
<input type="checkbox" id="east" class="checkall" value="5">
<label for="east">Check All</label>
<section id="check3">
<input type="checkbox" id="checkItem7" value="6" class="chk_east">
<label for="checkItem7">Item 1</label>
<input type="checkbox" id="checkItem8" value="6" class="chk_east">
<label for="checkItem8">Item 2</label>
<input type="checkbox" id="checkItem9" value="6" class="chk_east">
<label for="checkItem9">Item 3</label>
</section>
<p>You have selected:</p>
<div id="checked"></div>
I have the following group of checkboxes, and if a box is checked I want the value of the checkbox 'copied' to a hidden text field. So if the first, third and seventh box are checked the value of the hidden text field would be 1,3,7
I found this solution but that only adds one value to the textfield.
<input id=myhidden value="">
<div class="btn-group">
<button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
Tags <span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-form" role="menu">
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="1"> Lokaal</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="2"> Nationaal</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="3"> Beide</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="4"> Onbekend</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="5"> Lokale partij</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="6"> CDA</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="7"> VVD</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="8"> D66</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="9"> PvdA</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="10"> SP</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="11"> GroenLinks</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="12"> ChristenUnie</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="13"> SGP</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="14"> PVV</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="15"> Partij voor de Dieren</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="16"> Overige landelijke partij</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="17"> Politieke actor</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="18"> Pers</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="19"> Kiezer</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="20"> tweet</li>
<li><input class="checkbox" id="tagCheckbox" name="tags[]" type="checkbox" value="21"> user</li>
</ul>
</div>
and the JS I tried
$('input[type="checkbox"]').change(function(){
$('#myhidden').val($(this).val());
});
How do I do this?
Update: And when a checkbox is unchecked the value should be removed.
With this simple code, you can do it :
//Bind change event
$('.checkbox').on('change', function(){
//Select every checked input and build an array
var values = $('.checkbox:checked').map(function(){
//Return the value for the array
return this.value;
}).get();
//Set the new val to you input
$('#hiddenValue').val(values.join(','));
//See the result
console.log($('#hiddenValue').val())
});
Fiddle
You need to add one hidden field, and use the following code in the document ready event of jQuery.
Javascript
$(".checkbox").change(function(){
var oChecked = new Array();
$(".checkbox").each(function(index, val){
if($(val).is(":checked"))
oChecked.push($(val).val());
});
$("#hdnData").val(oChecked.join());
});
Html
<input type="hidden" id="hdnData" />
You can check it here http://jsfiddle.net/iamrmin/V2B6g/
I have also append the checked IDs to textarea so that you can feel the correctness.
I made this fiddle for you. Every time you click a checkbox, it will alert the result, which you can assign to your hidden field. Here is a code:
$('.checkbox').click(function() {
var result = "";
$('.checkbox').each(function() {
if ( this.checked ) {
if ( result.length > 0 ) {
result+=",";
}
result+=this.value;
}
});
alert("Assign value of your hidden field: " + result);
});
i have a long list with a grouped items , my visitors choose items from this list and submit the form , i need to keep the checked items at the top of the list after they submit the form or on page load , because that make them know which items they have chosen before at any time they get into the form without scrolling the whole list
my list with checked items looks like :
<div class="main-div">
<div class="group-title"><span>group one title</span></div>
<div class="items"><input type="checkbox" id="item-1" value="1" name="item_name[]" ><label for="item-1">item1</label></div>
<div class="items"><input type="checkbox" id="item-2" value="2" name="item_name[]" ><label for="item-2">item2</label></div>
<div class="items"><input type="checkbox" id="item-3" value="3" name="item_name[]" checked ><label for="item-3">item3</label></div>
<div class="items"><input type="checkbox" id="item-4" value="4" name="item_name[]" ><label for="item-4">item4</label></div>
<div class="items"><input type="checkbox" id="item-5" value="5" name="item_name[]" checked ><label for="item-5">item4</label></div>
<div class="group-title"><span>group two title</span></div>
<div class="items"><input type="checkbox" id="item-6" value="6" name="item_name[]" ><label for="item-6">item6</label></div>
<div class="items"><input type="checkbox" id="item-7" value="7" name="item_name[]" ><label for="item-7">item7</label></div>
<div class="items"><input type="checkbox" id="item-8" value="8" name="item_name[]" checked ><label for="item-8">item8</label></div>
<div class="items"><input type="checkbox" id="item-9" value="9" name="item_name[]"checked ><label for="item-9">item9</label></div>
<div class="items"><input type="checkbox" id="item-10"value="10" name="item_name[]"><label for="item-10">item10</label></div>
</div>
i need it to look like a following :
<div class="main-div">
<div class="items"><input type="checkbox" id="item-3" value="3" name="item_name[]" checked ><label for="item-3">item3</label></div>
<div class="items"><input type="checkbox" id="item-5" value="5" name="item_name[]" checked ><label for="item-5">item4</label></div>
<div class="items"><input type="checkbox" id="item-8" value="8" name="item_name[]" checked ><label for="item-8">item8</label></div>
<div class="items"><input type="checkbox" id="item-9" value="9" name="item_name[]" checked ><label for="item-9">item9</label></div>
<div class="group-title"><span>group one title</span></div>
<div class="items"><input type="checkbox" id="item-1" value="1" name="item_name[]" ><label for="item-1">item1</label></div>
<div class="items"><input type="checkbox" id="item-2" value="2" name="item_name[]" ><label for="item-2">item2</label></div>
<div class="items"><input type="checkbox" id="item-4" value="4" name="item_name[]" ><label for="item-4">item4</label></div>
<div class="group-title"><span>group two title</span></div>
<div class="items"><input type="checkbox" id="item-6" value="6" name="item_name[]" ><label for="item-6">item6</label></div>
<div class="items"><input type="checkbox" id="item-7" value="7" name="item_name[]" ><label for="item-7">item7</label></div>
<div class="items"><input type="checkbox" id="item-10" value="10" name="item_name[]"><label for="item-10">item10</label></div>
</div>
Try
jQuery(function($){
$('.main-div .group-title').each(function(){
$(this).nextUntil('.group-title', '.items').data('group', this);
});
$('.main-div .items').has('input:checked').prependTo('.main-div');
$('.main-div').on('change', 'input',function(){
var $item = $(this).closest('.items');
if(this.checked){
$item.insertBefore($('.main-div .group-title').first())
}else{
$item.appendTo($item.data('group'))
}
});
})
Demo: Fiddle
You can actually use jQuery's before and after code. It looks like this:
function swapElements(elm1, elm2) {
var parent1, next1,
parent2, next2;
parent1 = elm1.parentNode;
next1 = elm1.nextSibling;
parent2 = elm2.parentNode;
next2 = elm2.nextSibling;
parent1.insertBefore(elm2, next1);
parent2.insertBefore(elm1, next2);
}
This is just the skeleton. Nice question, so I am working on this.
Okay, so I have a bit of a problem. I have some code, the JS works (at least with the non-CSS modified version), but it fails with the CSS modified version. Wondering if anyone can lend me some help to figure out what went wrong with the implementation of the CSS...
What I want to have happen, is the any of the first 4 chekboxes, if checked, disable all others. But, if any of the other checkboxes (numbers 5 and up) are checked, none are disabled (unless of course someone checks one of the first four).
My JavaScript:
<script type="text/javascript">
$(window).load(function(){
var $inputs = $('input[type=checkbox]', $('#test'));
var $inputs2 = $('input[type=checkbox]', $('#test2'));
$inputs.change(function(){
var $this = $(this);
$inputs.not(this).prop('disabled',($this.index() < 4 && this.checked));
if($this.index() < 4 && this.checked){
$inputs.not(this).prop('checked',false);
}
});
$inputs2.change(function(){
var $this = $(this);
$inputs2.not(this).prop('disabled',($this.index() < 4 && this.checked));
if($this.index() < 4 && this.checked){
$inputs2.not(this).prop('checked',false);
}
});
});
The first Div:
<div id="test" style="float:left; width:50%">
<table border="0" cellpadding="0" cellspacing="0">
<tr><td style="font-size:3px"> </td></tr>
<tr>
<p id="Error" style="background: #ff0000; color: #fff;">Please, enter data</p></tr>
<td width="150px"><input type="checkbox" id="1" name="1" class="css-checkbox" value="1" />
<label for="1" class="css-label">1</label></td></tr><tr>
<td width="150px"><input type="checkbox" id="2" name="2" class="css-checkbox" value="2" />
<label for="2" class="css-label">2</label></td></tr><tr>
<td width="150px"><input type="checkbox" id="3" name="3" class="css-checkbox" value="3" />
<label for="3" class="css-label">3</label></td></tr><tr>
<td width="150px"><input type="checkbox" id="4" name="4" class="css-checkbox" value="4" />
<label for="4" class="css-label">4</label></td></tr><tr>
<td width="150px"><input type="checkbox" id="5" name="5" class="css-checkbox" value="5" />
<label for="5" class="css-label">5</label></td></tr><tr>
<td width="150px"><input type="checkbox" id="6" name="6" class="css-checkbox" value="6" />
<label for="6" class="css-label">6</label></td></tr>
</table>
</div>
My CSS, if needed:
<style>
#Error {display: none;}
input[type=checkbox].css-checkbox {display:none;}
input[type=checkbox].css-checkbox + label.css-label {padding-left:20px;height:20px;display:inline-block;line-height:20px;background-repeat:no-repeat;background-position: 0 0;font-size:15px;vertical-align:middle;cursor:pointer;}
input[type=checkbox].css-checkbox:checked + label.css-label {background-position: 0 -20px;}
input[type=checkbox].css-checkbox:disabled + label.css-label {background-position: 0 -40px;}
.css-label{ background-image:url(web-two-style.png);}
</style>
The problem is: the following works, and the following was later modified with CSS and I can't figure out what went wrong.
What works:
<div id='test'>
<input type="checkbox" name="check1" value="1" id="check1" >first
<input type="checkbox" name="check2" value="1" id="check2" >second
<input type="checkbox" name="check3" value="1" id="check3" >third
<input type="checkbox" name="check4" value="1" id="check4" >fourth
<input type="checkbox" name="check5" value="1" id="check5" >fifth
<input type="checkbox" name="check6" value="1" id="check6" >sixth
<input type="checkbox" name="check7" value="1" id="check7" >seventh
<input type="checkbox" name="check8" value="1" id="check8" >eight
</div>
It gets even odder when I add the code that worked to the same div as the one that doesn't, since it the above non-CSS then works properly and will disable (where appropriate) all the checkboxes regardless of CSS, but when the CSS are checked, all are disabled:
<div id="test" style="float:left; width:50%">
<table border="0" cellpadding="0" cellspacing="0">
<tr><td style="font-size:3px"> </td></tr>
<tr>
<p id="Error" style="background: #ff0000; color: #fff;">Please, enter data</p></tr>
<td width="150px"><input type="checkbox" id="1" name="1" class="css-checkbox" value="1" />
<label for="1" class="css-label">1</label></td></tr><tr>
<td width="150px"><input type="checkbox" id="2" name="2" class="css-checkbox" value="2" />
<label for="2" class="css-label">2</label></td></tr><tr>
<td width="150px"><input type="checkbox" id="3" name="3" class="css-checkbox" value="3" />
<label for="3" class="css-label">3</label></td></tr><tr>
<td width="150px"><input type="checkbox" id="4" name="4" class="css-checkbox" value="4" />
<label for="4" class="css-label">4</label></td></tr><tr>
<td width="150px"><input type="checkbox" id="5" name="5" class="css-checkbox" value="5" />
<label for="5" class="css-label">5</label></td></tr><tr>
<td width="150px"><input type="checkbox" id="6" name="6" class="css-checkbox" value="6" />
<label for="6" class="css-label">6</label></td></tr> <input type="checkbox" name="check1" value="1" id="check1" >first
<input type="checkbox" name="check2" value="1" id="check2" >second
<input type="checkbox" name="check3" value="1" id="check3" >third
<input type="checkbox" name="check4" value="1" id="check4" >fourth
<input type="checkbox" name="check5" value="1" id="check5" >fifth
<input type="checkbox" name="check6" value="1" id="check6" >sixth
<input type="checkbox" name="check7" value="1" id="check7" >seventh
<input type="checkbox" name="check8" value="1" id="check8" >eight
</table>
</div>
.index() returns the index relative to the sibling elements. In your working example the siblings of an <input> are the other inputs. In your non-working example the only sibling is the <label> element.
In your "odd" example all inputs are siblings again.
In order to make it work anyway you can use .index(element):
$inputs.index($this)
This returns the index of the input $this within the jquery input collection $inputs. Working fiddle.
I would change the ID's on your input fields, ID's can't start with a number
<input type="checkbox" id="5" name="5" class="css-checkbox" value="5" />
may cause problems with your JS. I would try something like
<input type="checkbox" id="field-5" name="5" class="css-checkbox" value="5" />