CSS - How can i make first row always fixed? - javascript

I have an unordered list(<ul>) where i have a few elements. Now in my list at the top I have select all and a Go button. I want Select all and Go button fixed indepedent of the scrollbar. I tried giving position:fixed to the (li) of Select All but when the values increase the position of Select All distorts. Please guide.
.scrollClass {
overflow: auto;
}
.scrollClass select {
border: none;
}
.dropDown, testClass {
padding-left: 0px !important;
z-index: 999999999;
position: absolute;
}
.rowClass {
cursor: pointer;
padding: 5px;
border: 1px solid #94c0d2;
list-style: none;
border-bottom-style: none;
}
<h3>Name</h3>
<ul class="testClass" style=" padding-left:0px; margin-bottom:0px;">
<li>
<input type="text" name="name" id="myText">
</li>
</ul>
<ul class="dropDown" style="max-height:200px; overflow-y:scroll; margin-top:2px;">
<li class="rowClass" style=" max-width:400px; min-width:400px; ">
<label>
<input type="checkbox" id="check" value="0"> Select All
</label>
<input type="button" value="Go" style="float:right;">
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test1
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test2
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test3
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test4
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test5
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test5
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test5
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test5
</label>
</li>
</ul>

Move Select All input field in above <ul> tag
.scrollClass{
overflow: auto;
}
.scrollClass select{
border: none;
}
.dropDown, testClass{
padding-left:0px !important;
z-index:999999999;
position:absolute;
}
.rowClass{
cursor:pointer;
padding:5px;
border:1px solid #94c0d2;
list-style:none;
border-bottom-style:none;
}
<h3>
Name
</h3>
<ul class="testClass" style=" padding-left:0px; margin-bottom:0px;">
<li>
<input type="text" autocomplete="off" name="name" id="myText"/>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" id="chkAll" value="0"/> Select All
</label>
<input type="button" value="Go" style="float:right;"></input>
</li>
</ul>
<ul class="dropDown" style="max-height:200px; overflow-y:scroll; margin-top:2px;">
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"/> Test
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"/> Test1
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"/> Test2
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"/> Test3
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"/> Test4
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"/> Test5
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"/> Test6
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"/> Test7
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"/> Test8
</label>
</li>
</ul>

You could add another ul after the Select All li and modify the inline-styling for it and its parent ul.
ul {padding:0}
.scrollClass {
overflow: auto;
}
.scrollClass select {
border: none;
}
.dropDown, testClass {
padding-left: 0px !important;
z-index: 999999999;
position: absolute;
}
.rowClass {
cursor: pointer;
padding: 5px;
border: 1px solid #94c0d2;
list-style: none;
border-bottom-style: none;
}
<h3>Name</h3>
<ul class="testClass" style="padding-left:0px; margin-bottom:0px;">
<li>
<input type="text" autocomplete="off" name="name" id="myText">
</li>
</ul>
<ul class="dropDown" style="margin-top:2px;">
<li class="rowClass" style="width:400px">
<label>
<input type="checkbox" id="chkAll" value="0"> Select All
</label>
<input type="button" value="Go" style="float:right;">
</li>
<ul style="max-height:200px; overflow-y:scroll;">
<li class="rowClass" style="max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test
</label>
</li>
<li class="rowClass" style="max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test1
</label>
</li>
<li class="rowClass" style="max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test2
</label>
</li>
<li class="rowClass" style="max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test3
</label>
</li>
<li class="rowClass" style="max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test4
</label>
</li>
<li class="rowClass" style="max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test5
</label>
</li>
<li class="rowClass" style="max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test6
</label>
</li>
<li class="rowClass" style="max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test7
</label>
</li>
<li class="rowClass" style="max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test8
</label>
</li>
</ul>
</ul>

though my vote goes to #Satendra but please also Try this
.scrollClass {
overflow: auto;
}
.scrollClass select {
border: none;
}
.dropDown, testClass {
padding-left: 0px !important;
z-index: 999999999;
position: absolute;
}
.rowClass {
cursor: pointer;
padding: 5px;
border: 1px solid #94c0d2;
list-style: none;
border-bottom-style: none;
}
.rowClass:first-child {
position: fixed;
background: #ffffff;
border: 1px solid #94c0d2;
}
<h3>Name</h3>
<ul class="testClass" style=" padding-left:0px; margin-bottom:0px;">
<li>
<input type="text" autocomplete="off" name="name" id="myText">
</li>
</ul>
<ul class="dropDown" style="max-height:200px; overflow-y:scroll; margin-top:2px;">
<li class="rowClass" style=" max-width:400px; min-width:400px; ">
<label>
<input type="checkbox" id="chkAll" value="0"> Select All
</label>
<input type="button" value="Go" style="float:right;">
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test1
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test2
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test3
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test4
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test5
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test5
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test5
</label>
</li>
<li class="rowClass" style=" max-width:400px; min-width:400px;">
<label>
<input type="checkbox" name="allClass" class="checkBoxClass"> Test5
</label>
</li>
</ul>

Related

Click event on same button class, but invokes different action in jquery

I am having an issue. I have the code defined here:
$('.js-cars-item [type="checkbox"]').change(function() {
var idx = $(this).closest('li').index(); //Get the index - Number in order
var chk = $(this).is(":checked"); //Get if checked or not
var obj = this; //Checkbox object
$('.js-cars-item').each(function() { //Loop every js-cars-item
//Find the checkbox with the same index of clicked checkbox. Change disabled property
$(this).find('li:eq(' + idx + ') [type="checkbox"]').not(obj).prop("checked", false);
});
var checkeds = [];
$(".cars-item input:checkbox:checked").each(function(index) {
checkeds[index] = $(this).attr('id');
});
console.clear();
console.log("These are checked:", checkeds);
})
$('.js-add-category').click(function() {
var categoryContent = `<div class="cars">
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-3">
<label for="car-1-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-3">
<label for="car-2-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-3">
<label for="car-3-3"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<button type="button" class="js-add-section">Add Section</button>
</div> <br>`
$('.main').append(categoryContent);
});
$(document.body).on('click', 'button.js-add-section', function() {
var sectionContent = `<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-6>
<label for="car-1-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-6>
<label for="car-2-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-6>
<label for="car-3-6"><i class="icon-tick"></i></label>
</li>
</ul> </div>`
$('.cars').append(sectionContent);
});
.cars-item {
border-bottom: 1px solid gray;
}
ul {
/* Added to fully show console in snippet */
margin: 2px;
}
li {
display: inline;
}
.cars {
box-sizing: content-box;
width: 250px;
height: auto;
padding: 10px;
border: 5px solid blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="js-add-category">Add Category</button> <br> <br>
<div class="main">
<div class="cars">
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-3">
<label for="car-1-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-3">
<label for="car-2-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-3">
<label for="car-3-3"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<button type="button" class="js-add-section">Add Section</button>
<br>
<div class="section">
</div>
</div>
<br>
The issue is I am facing that, when I click on the Add Section button in a box, it adds duplicate rows in the other boxes. What I want to achieve is to add a row of checkboxes only in the box that I have clicked the 'Add Section' button.
When I click Add Category, it generates another box with another button Add Section
Please demo the code to get an overview. Please someone help me with this, I am totally stuck.
Thanks.
Just use $(this).parent().append(sectionContent); instead of $('.cars').append(sectionContent);
Working code
$('.js-cars-item [type="checkbox"]').change(function() {
var idx = $(this).closest('li').index(); //Get the index - Number in order
var chk = $(this).is(":checked"); //Get if checked or not
var obj = this; //Checkbox object
$('.js-cars-item').each(function() { //Loop every js-cars-item
//Find the checkbox with the same index of clicked checkbox. Change disabled property
$(this).find('li:eq(' + idx + ') [type="checkbox"]').not(obj).prop("checked", false);
});
var checkeds = [];
$(".cars-item input:checkbox:checked").each(function(index) {
checkeds[index] = $(this).attr('id');
});
console.clear();
console.log("These are checked:", checkeds);
})
$('.js-add-category').click(function() {
var categoryContent = `<div class="cars">
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-3">
<label for="car-1-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-3">
<label for="car-2-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-3">
<label for="car-3-3"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<button type="button" class="js-add-section">Add Section</button>
</div> <br>`
$('.main').append(categoryContent);
});
$(document.body).on('click', 'button.js-add-section', function() {
var sectionContent = `<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-6>
<label for="car-1-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-6>
<label for="car-2-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-6>
<label for="car-3-6"><i class="icon-tick"></i></label>
</li>
</ul> </div>`
$(this).parent().append(sectionContent);
});
.cars-item {
border-bottom: 1px solid gray;
}
ul {
/* Added to fully show console in snippet */
margin: 2px;
}
li {
display: inline;
}
.cars {
box-sizing: content-box;
width: 250px;
height: auto;
padding: 10px;
border: 5px solid blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="js-add-category">Add Category</button> <br> <br>
<div class="main">
<div class="cars">
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-3">
<label for="car-1-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-3">
<label for="car-2-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-3">
<label for="car-3-3"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<button type="button" class="js-add-section">Add Section</button>
<br>
<div class="section">
</div>
</div>
<br>
$(".cars") refers to all .cars elements. You only want the one that contains the button you clicked on. That would be $(this).closest(".cars")
To make the validation code work on newly added sections, you need to use event delegation, just like you do for the .js-add-section click handler.
$(".main").on('change', '.js-cars-item [type="checkbox"]', function() {
var idx = $(this).closest('li').index(); //Get the index - Number in order
var chk = $(this).is(":checked"); //Get if checked or not
var obj = this; //Checkbox object
$(this).closest('.cars').find('.js-cars-item').each(function() { //Loop every js-cars-item
//Find the checkbox with the same index of clicked checkbox. Change disabled property
$(this).find('li:eq(' + idx + ') [type="checkbox"]').not(obj).prop("checked", false);
});
var checkeds = [];
$(this).closest(".cars").find(".cars-item input:checkbox:checked").each(function(index) {
checkeds[index] = $(this).attr('id');
});
console.clear();
console.log("These are checked:", checkeds);
})
$('.js-add-category').click(function() {
var categoryContent = `<div class="cars">
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-3">
<label for="car-1-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-3">
<label for="car-2-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-3">
<label for="car-3-3"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<button type="button" class="js-add-section">Add Section</button>
</div> <br>`
$('.main').append(categoryContent);
});
$(document.body).on('click', 'button.js-add-section', function() {
var sectionContent = `<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-6>
<label for="car-1-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-6>
<label for="car-2-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-6>
<label for="car-3-6"><i class="icon-tick"></i></label>
</li>
</ul> </div>`
$(this).closest('.cars').append(sectionContent);
});
.cars-item {
border-bottom: 1px solid gray;
}
ul {
/* Added to fully show console in snippet */
margin: 2px;
}
li {
display: inline;
}
.cars {
box-sizing: content-box;
width: 250px;
height: auto;
padding: 10px;
border: 5px solid blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="js-add-category">Add Category</button> <br> <br>
<div class="main">
<div class="cars">
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-3">
<label for="car-1-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-3">
<label for="car-2-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-3">
<label for="car-3-3"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<button type="button" class="js-add-section">Add Section</button>
<br>
<div class="section">
</div>
</div>
<br>

Checking checkboxes in jquery validation

I have several rows of checkboxes. The scenario is that when I click a checkbox in the first row, and when I click another checkbox in the second or third row and within the same column, the previous checked checkbox is unchecked and the new one is checked. This is working fine. My issue is that when I uncheck the newly checked checkbox, it should re-check the previous checkbox checked. Anyone who can help me with this please ?
The demo is as per below.
$('.js-cars-item [type="checkbox"]').change(function() {
var idx = $(this).closest('li').index(); //Get the index - Number in order
var chk = $(this).is(":checked"); //Get if checked or not
var obj = this; //Checkbox object
$('.js-cars-item').each(function() { //Loop every js-cars-item
//Find the checkbox with the same index of clicked checkbox. Change disabled property
$(this).find('li:eq(' + idx + ') [type="checkbox"]').not(obj).prop("checked", false);
});
var checkeds = [];
$(".cars-item input:checkbox:checked").each(function(index) {
checkeds[index] = $(this).attr('id');
});
console.clear();
console.log("These are checked:", checkeds);
})
.cars-item {
border-bottom: 1px solid gray;
}
ul {
/* Added to fully show console in snippet */
margin: 2px;
}
li {
display: inline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-3">
<label for="car-1-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-3">
<label for="car-2-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-3">
<label for="car-3-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-4-3">
<label for="car-4-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-5-3">
<label for="car-5-3"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-4">
<label for="car-1-4"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-4">
<label for="car-2-4"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-4">
<label for="car-3-4"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-4-4">
<label for="car-4-4"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-5-4">
<label for="car-5-4"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-5">
<label for="car-1-5"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-5">
<label for="car-2-5"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-5">
<label for="car-3-5"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-4-5">
<label for="car-4-5"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-5-5">
<label for="car-5-5"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-6>
<label for="car-1-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-6">
<label for="car-2-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-6">
<label for="car-3-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-4-6">
<label for="car-4-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-5-6">
<label for="car-5-6"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-7>
<label for="car-1-7"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-7">
<label for="car-2-7"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-7">
<label for="car-3-7"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-4-7">
<label for="car-4-7"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-5-7">
<label for="car-5-7"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-8">
<label for="car-1-8"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-8">
<label for="car-2-8"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-8">
<label for="car-3-8"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-4-8">
<label for="car-4-8"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-5-8">
<label for="car-5-8"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
I have updated your javascript code, please try this:
$('.js-cars-item [type="checkbox"]').change(function() {
var idx = $(this).closest('li').index(); //Get the index - Number in order
var chk = $(this).is(":checked"); //Get if checked or not
var obj = this; //Checkbox object
if(chk)
{ //If it is checked
$('.previous_'+idx).removeClass('previous_'+idx); //Remove the 'previous_$index' class from the checkbox
$('.js-cars-item').find('li:eq(' + idx + ') [type="checkbox"]:checked').not(obj).addClass('previous_'+idx).prop("checked", false); //set the 'previous_$index' class to an existing checked checkbox and remove the checked property
}
else if($('.previous_'+idx).length)
{ //If the 'previous_$index' class is available while uncheck the current checkbox
$('.previous_'+idx).prop("checked", true).removeClass('previous_'+idx); //set the 'previous_$index' class's checkbox to checked and remove this class
$(obj).addClass('previous_'+idx); //set the 'previous_$index' class to the currently unchecked checkbox
}
var checkeds = [];
$(".cars-item input:checkbox:checked").each(function(index) {
checkeds[index] = $(this).attr('id');
});
console.clear();
console.log("These are checked:", checkeds);
})
.cars-item {
border-bottom: 1px solid gray;
}
ul {
/* Added to fully show console in snippet */
margin: 2px;
}
li {
display: inline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-3">
<label for="car-1-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-3">
<label for="car-2-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-3">
<label for="car-3-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-4-3">
<label for="car-4-3"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-5-3">
<label for="car-5-3"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-4">
<label for="car-1-4"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-4">
<label for="car-2-4"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-4">
<label for="car-3-4"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-4-4">
<label for="car-4-4"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-5-4">
<label for="car-5-4"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-5">
<label for="car-1-5"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-5">
<label for="car-2-5"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-5">
<label for="car-3-5"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-4-5">
<label for="car-4-5"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-5-5">
<label for="car-5-5"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<div class="cars-item js-cars-item">
<ul> </
<li>
<input type="checkbox" id="car-1-6">
<label for="car-1-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-6">
<label for="car-2-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-6">
<label for="car-3-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-4-6">
<label for="car-4-6"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-5-6">
<label for="car-5-6"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-7">
<label for="car-1-7"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-7">
<label for="car-2-7"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-7">
<label for="car-3-7"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-4-7">
<label for="car-4-7"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-5-7">
<label for="car-5-7"><i class="icon-tick"></i></label>
</li>
</ul>
</div>
<div class="cars-item js-cars-item">
<ul>
<li>
<input type="checkbox" id="car-1-8">
<label for="car-1-8"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-2-8">
<label for="car-2-8"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-3-8">
<label for="car-3-8"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-4-8">
<label for="car-4-8"><i class="icon-tick"></i></label>
</li>
<li>
<input type="checkbox" id="car-5-8">
<label for="car-5-8"><i class="icon-tick"></i></label>
</li>
</ul>
</div>

if radio button checked change parent background color

I have four columns of radio lists. I want to change background color and border of selected item. I have applied some stackoverflow answers on my code like
If radio button checked change parent div (li) background - javascript
Changing background color with CSS on radio button input when :checked
But as my structure is having four columns its not working properly. Sometimes if I select one language from column first then last option of each other column gets background color. Sometimes multiple radio buttons are gets selected.
I am not getting how I should apply jquery/javascript to change color of my selected li item in any other way. Can anyone please help me in this? Solutions other than I had tried will be appreciated. Thanks.
HTML code:
<div class="added">
<div class="column-left">
<ul class="options-list">
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" data-toggle="radio">
English
</label>
</li>
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio" >
<input type="radio" name="optionsRadios" id="optionsRadios2" value="option2" data-toggle="radio">
German
</label>
</li>
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio" >
<input type="radio" name="optionsRadios" id="optionsRadios3" value="option3" data-toggle="radio">
Japanese
</label>
</li>
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio" >
<input type="radio" name="optionsRadios" id="optionsRadios4" value="option4" data-toggle="radio">
Russian
</label>
</li>
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio" >
<input type="radio" name="optionsRadios" id="optionsRadios5" value="option5" data-toggle="radio">
Other
</label>
</li>
</ul>
</div>
<div class="column-center">
<ul class="options-list">
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios6" value="option6" data-toggle="radio">
Arabic
</label>
</li>
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios7" value="option7" data-toggle="radio">
Hebrew
</label>
</li>
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios8" value="option8" data-toggle="radio">
Korean
</label>
</li>
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios9" value="option9" data-toggle="radio">
Spanish
</label>
</li>
</ul>
</div>
<div class="column-center-right">
<ul class="options-list">
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios10" value="option10" data-toggle="radio">
Chinese
</label>
</li>
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios11" value="option11" data-toggle="radio">
Hindi
</label>
</li>
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios12" value="option12" data-toggle="radio">
Polish
</label>
</li>
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios13" value="option13" data-toggle="radio">
Taiwanese
</label>
</li>
</ul>
</div>
<div class="column-right">
<ul class="options-list">
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios14" value="option14" data-toggle="radio">
French
</label>
</li>
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios15" value="option15" data-toggle="radio">
Italian
</label>
</li>
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios16" value="option16" data-toggle="radio">
Portuguese
</label>
</li>
<li class="ng-scope" ng-repeat="way in group.paymentWays" ng-switch="" on="way.type">
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios17" value="option17" data-toggle="radio">
Vietnamese
</label>
</li>
</ul>
</div>
</div>
CSS code:
ul.options-list {
margin: 0;
display: block;
list-style: none;
font-family: Arial;
padding-left: 110px;
text-align: left;
width: 215px;
}
ul.options-list li {
position: relative;
padding: 17px 17px;
background-color: #F6F6F6;
color: #2a4753;
/* cursor: pointer;*/
max-height: 150px;
overflow: hidden;
border: 1px solid #DDDDDD;
border-radius: 8px;
margin-bottom: 16px;
margin-top: 15px;
}
div.added {
padding: 0px 0px 5px 0px;
font-size: 22px;
font-family: Arial;
color: #2a4753;
text-align: left;
}
.column-left{ float: left; width: 22%; text-align: center;}
.column-right{ float: left; width:22%;text-align: center; }
.column-center{ float: left; width: 22%; text-align: center;}
.column-center-right{ float: left; width: 22%; text-align: center;}
JS and CSS code that I had tried:
function updateHighlightRadio() {
var selected = this.parentNode.parentNode.parentNode.getElementsByClassName("selected")[0];
if (selected) selected.className = selected.className.replace(" selected", "");
this.parentNode.parentNode.className += " selected";
}
window.onload = function () {
var radios = document.querySelectorAll("input[type=radio]");
for (var i = 0; i < radios.length; ++i) {
radios[i].onchange = updateHighlightRadio;
}
}
.options-list .selected {
background-color: #E8F2F4;
}
In above code I had tried by changing name of my all 4 column's ul class and tried by grouping/giving same name of radio buttons for each column and accordingly changing js code but its not working properly.
$(document).ready(function() {
$("ul.options-list li").on("click",function() {
$('ul.options-list li').removeClass('sel_bk_color');
$(this).addClass('sel_bk_color');
});
});
.sel_bk_color{
background-color: #E8F2F4;
}
Above code also not working.
Try like this
$(document).ready(function() {
$("ul.options-list li").on("click",function() {
if($(this).find('input[type="radio"]').is(':checked')) {
$('ul.options-list li').removeClass('sel_bk_color');
$(this).addClass('sel_bk_color');
}
});
});
Play Here https://jsfiddle.net/tr9Lyxz3/1/

Issue in multi select filter

I need to filter using multiselct ,data attribute . it is working but not as expected
here is my code
<ul>
<li>
<div class="sts">
<h1>filter 1</h1>
<div class="checkbox">
<label>
<input data-id="1" data-type="st" class="stat st" type="checkbox" />1
</label>
</div>
<div class="checkbox">
<label>
<input data-id="2" data-type="st" class="stat st" type="checkbox" />2
</label>
</div>
<div class="checkbox">
<label>
<input data-id="3" data-type="st" class="stat st" type="checkbox" />3
</label>
</div>
</div>
</li>
<li>
<h1>filter 2</h1>
<div class="ats">
<div class="checkbox">
<label>
<input data-id="foo" data-type="at" class="stat at" type="checkbox" />foo
</label>
</div>
<div class="checkbox">
<label>
<input data-id="boo" data-type="at" class="stat at" type="checkbox" />boo
</label>
</div>
<div class="checkbox">
<label>
<input data-id="bar" data-type="at" class="stat at" type="checkbox" />bar
</label>
</div>
</div>
</li>
<li>
<h1>filter 3</h1>
<div class="dpts">
<div class="checkbox">
<label>
<input data-id="a" data-type="dpt" class="stat dpt" type="checkbox" />a
</label>
</div>
<div class="checkbox">
<label>
<input data-id="b" data-type="dpt" class="stat dpt" type="checkbox" />b
</label>
</div>
<div class="checkbox">
<label>
<input data-id="c" data-type="dpt" class="stat dpt" type="checkbox" />c
</label>
</div>
<div class="checkbox">
<label>
<input data-id="d" data-type="dpt" class="stat dpt" type="checkbox" />d
</label>
</div>
</div>
</li>
</ul>
<ul class="list">
<li data-at="foo" data-st="1" data-dpt="a">asdw</li>
<li data-at="boo" data-st="2" data-dpt="c">qwedf</li>
<li data-at="boo" data-st="1" data-dpt="a">qwedf</li>
<li data-at="bar" data-st="3" data-dpt="b">tazxsw</li>
<li data-at="bar" data-st="1" data-dpt="b">zxcvb</li>
<li data-at="foo" data-st="1" data-dpt="b">poiuy</li>
<li data-at="boo" data-st="2" data-dpt="d">lkjhg</li>
<li data-at="boo" data-st="3" data-dpt="d">lkjhg</li>
</ul>
Here is my script
<script>
$('.stat').on('click', function() {
var $stats = $('.stat:checked');
var $items = $('.list li');
if ($stats.length == 0) {
$items.show();
return;
}
$items.hide();
$stats.each(function() {
var $stat = $(this);
$items.filter(function() {
return $(this).data($stat.data('type')) == $stat.data('id');
}).show();
})
});
</script>
If i select any one from each fliter it is working fine , but when i select from same filter it should perform logical OR but it is performing logical AND.
Here is the fiddler lik
I'm struggling with this same problem as durai. I updated the fiddle, please make selections as instructed at the top of the page and you'll see the problematic case.
<ul class="list">
<li data-at="foo" data-st="1" data-dpt="a">asdw</li>
<li data-at="boo" data-st="3" data-dpt="c">qwedf</li>
<li data-at="boo" data-st="1" data-dpt="a">qwedf</li>
<li data-at="bar" data-st="3" data-dpt="a">this should be visible (when filters: 3 | bar | a&b)</li>
<li data-at="bar" data-st="2" data-dpt="b">zxcvb</li>
<li data-at="foo" data-st="1" data-dpt="b">poiuy</li>
<li data-at="boo" data-st="2" data-dpt="d">lkjhg</li>
<li data-at="bar" data-st="3" data-dpt="b">this should be visible (when filters: 3 | bar | a&b)</li>

Check the parent checkbox with jQuery

Basically I am trying to write a js func that if I check a child checkbox the parent checkbox also checks, if not already checked. I am using jquery here is my html:
<ul id="ulParent" style="margin: 0; padding: 0; list-style: none;">
<li style="margin: 0 0 5px 0;">
<input type="checkbox" checked="checked" class="liParent" id="p1" />Parent1
<ul id="ulParent" style="margin: 0; padding: 0; list-style: none;">
<li>
<input type="checkbox" checked="checked" class="liChild" id="c1" onclick="checkParent(this);" />Child1
</li>
<li>
<input type="checkbox" checked="checked" class="liChild" id="c2" onclick="checkParent(this);"/>Child2
</li>
<li>
<input type="checkbox" checked="checked" class="liChild" id="c3" onclick="checkParent(this);"/>Child3
</li>
<li>
<input type="checkbox" checked="checked" class="liChild" id="c4" onclick="checkParent(this);"/>Child4
</li>
</ul>
</li>
<li style="margin: 0 0 5px 0;">
<input type="checkbox" checked="checked" class="liParent" id="p2" />Parent2
<ul id="ulParent" style="margin: 0; padding: 0; list-style: none;">
<li>
<input type="checkbox" checked="checked" class="liChild" id="c1" onclick="checkParent(this);" />Child1
</li>
<li>
<input type="checkbox" checked="checked" class="liChild" id="c2" onclick="checkParent(this);"/>Child2
</li>
<li>
<input type="checkbox" checked="checked" class="liChild" id="c3" onclick="checkParent(this);"/>Child3
</li>
<li>
<input type="checkbox" checked="checked" class="liChild" id="c4" onclick="checkParent(this);"/>Child4
</li>
</ul>
</li>
</ul>
JS Func
function checkParent(child){
if (child != null) {
// if child is checked we need to check the parent category
if (child.checked) {
// get the parent checkbox and check it if not check....need help here....
$(child).parent().parent().parent()......
}
}
}
I'd remove the inline javascript in your HTML and use jQuery binding to bind the check event:
$(document).ready(function() {
$('input.liChild').change(function() {
if ($(this).is(':checked')) {
$(this).closest('ul').siblings('input:checkbox').attr('checked', true);
}
});
});
This will bind the event you're looking for to any input with the class liChild automatically without all those onclicks.
This will work, based on your structure:
$(child).parent().parent().prev().attr('checked', true);
or
$(child).closest('ul').prev().attr('checked', true);;

Categories