How to add check box checked count value - javascript

i need to add the total number of check box checked count value in element.
But when i checked the check box in "heading 2" part , the count value was added in "heading 1" part .
I did not find the issue any one please guide me resolve this issue
DEMO
HTMl:
<div id="main">
<div class="a">
<div class="row">
<div class="col-md-12 cb_select_head">
<a data-parent="#accordion" data-toggle="collapse" href="#function-collapse"><span class=
"glyphicon ecm-caret-down"></span></a><span class="labelBlock labelCounter">
<span class="count-checked-checkboxes">0</span></span>
<span class="fb_options_head">heading 1</span>
</div>
<ul class="sidebar fb-list-option collapse in" id="function-collapse">
<li><span class="glyphicon ecm-caret-right"></span>
<input id="c1" name="cc" type="checkbox">
<label for="c1">asdas</label>
</li>
<li class="noArrow">
<input id="c2" name="cc" type="checkbox">
<label for="c2">asdasd</label>
</li>
<li><span class="glyphicon ecm-caret-right"></span>
<input id="c3" name="cc" type="checkbox">
<label for="c3">asdasd
</label>
<ul>
<li><input id="c1" name="cc" type="checkbox"><label for="c1">asd 1</label></li>
<li><input id="c1" name="cc" type="checkbox"><label for="c1">sadas 2</label></li>
<li><input id="c1" name="cc" type="checkbox"><label for="c1">asdas 3</label></li>
</ul>
</li>
<li><span class="glyphicon ecm-caret-right"></span>
<input id="c4" name="cc" type="checkbox">
<label for="c4">asdasd</label>
</li>
<li class="noArrow">
<input id="c5" name="cc" type="checkbox">
<label for="c5">five</label>
</li>
</ul>
</div>
</div>
<!--First List End-->
<!--Second list start -->
<div class="a">
<div class="row">
<div class="col-md-12 cb_select_head">
<a data-parent="#accordion" data-toggle="collapse" href="#function-collapse"><span class=
"glyphicon ecm-caret-down"></span></a><span class="labelBlock labelCounter">
<span class="count-checked-checkboxes">0</span></span>
<span class="fb_options_head">heading 2</span>
</div>
<ul class="sidebar fb-list-option collapse in" id="function-collapse">
<li><span class="glyphicon ecm-caret-right"></span>
<input id="c1" name="cc" type="checkbox">
<label for="c1">asdas</label>
</li>
<li class="noArrow">
<input id="c2" name="cc" type="checkbox">
<label for="c2">asdasd</label>
</li>
<li><span class="glyphicon ecm-caret-right"></span>
<input id="c3" name="cc" type="checkbox">
<label for="c3">asdasd
</label>
<ul>
<li><input id="c1" name="cc" type="checkbox"><label for="c1">asd 1</label></li>
<li><input id="c1" name="cc" type="checkbox"><label for="c1">sadas 2</label></li>
<li><input id="c1" name="cc" type="checkbox"><label for="c1">asdas 3</label></li>
</ul>
</li>
<li><span class="glyphicon ecm-caret-right"></span>
<input id="c4" name="cc" type="checkbox">
<label for="c4">asdasd</label>
</li>
<li class="noArrow">
<input id="c5" name="cc" type="checkbox">
<label for="c5">five</label>
</li>
</ul>
</div>
</div>
</div>
JS:
$(document).ready(function() {
var $checkboxes = $(
'#main ul input[type="checkbox"]');
$checkboxes.change(function() {
var countCheckedCheckboxes = $checkboxes.filter(
':checked').length;
$('.count-checked-checkboxes').text(
countCheckedCheckboxes);
$('#edit-count-checked-checkboxes').val(
countCheckedCheckboxes);
});
});

see the working solution. Remember IDs needs to be unique, and avoid use of IDs whenever possible like putting input element inside the label instead of using with Id
$(document).ready(function() {
var $checkboxes1 = $('#head1 ul input[type="checkbox"]');
var $checkboxes2 = $('#head2 ul input[type="checkbox"]');
$checkboxes1.change(function() {
var countCheckedCheckboxes = $checkboxes1.filter(':checked').length;
$('#head1 .count-checked-checkboxes').text(countCheckedCheckboxes);
});
$checkboxes2.change(function() {
var countCheckedCheckboxes = $checkboxes2.filter(':checked').length;
$('#head2 .count-checked-checkboxes').text(countCheckedCheckboxes);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
<div id="head1">
<div class="row">
<div class="col-md-12 cb_select_head">
<span class="count-checked-checkboxes">0</span>
<span class="fb_options_head">heading 1</span>
</div>
<ul class="sidebar fb-list-option collapse in">
<li>
<label>
<input name="cc" type="checkbox">One
</label>
</li>
<li class="noArrow">
<label>
<input name="cc" type="checkbox">Two
</label>
</li>
<li>
<ul>
<li>
<label>
<input name="cc" type="checkbox">3 - a
</label>
</li>
<li>
<label>
<input name="cc" type="checkbox">3 -b
</label>
</li>
<li>
<label>
<input name="cc" type="checkbox">2 - c
</label>
</li>
</ul>
</li>
<li>
<label>
<input name="cc" type="checkbox">four
</label>
</li>
<li class="noArrow">
<label>
<input name="cc" type="checkbox">five
</label>
</li>
</ul>
</div>
</div>
<!--First List End-->
<!--Second list start -->
<div class="a" id="head2">
<div class="row">
<div class="col-md-12 cb_select_head">
<span class="count-checked-checkboxes">0</span>
<span class="fb_options_head">heading 1</span>
</div>
<ul class="sidebar fb-list-option collapse in">
<li>
<label>
<input name="cc" type="checkbox">One
</label>
</li>
<li class="noArrow">
<label>
<input name="cc" type="checkbox">Two
</label>
</li>
<li>
<ul>
<li>
<label>
<input name="cc" type="checkbox">3 - a
</label>
</li>
<li>
<label>
<input name="cc" type="checkbox">3 - b
</label>
</li>
<li>
<label>
<input name="cc" type="checkbox">2 - c
</label>
</li>
</ul>
</li>
<li>
<label>
<input name="cc" type="checkbox">four
</label>
</li>
<li class="noArrow">
<label>
<input name="cc" type="checkbox">five
</label>
</li>
</ul>
</div>
</div>
</div>

See if this works.
Ignoring everything others have mentioned, about ID's needing to be unique and what not, I was able to tally checked boxes for each heading using DOM traversal.
I changed the jQuery to:
$(document).ready(function() {
$('.a').each(function(){
var $checkboxes = $(this).find('input[type="checkbox"]');
$checkboxes.change(function() {
var countCheckedCheckboxes = $checkboxes.filter(':checked').length;
$(this).closest('.a').find('.count-checked-checkboxes').text(
countCheckedCheckboxes);
});
});
});
Loop through each element '.a'
Use $(this).find('input[type="checkbox"]'); to get just the relevant checkboxes
On change, traverse up the DOM until you find '.a', from there you can traverse down and find the heading class '.count-checked-checkboxes'
There's probably a better way to do this though.
.closest() starts at the current element and travels upwards looking for a match.
.find() travels downwards through descendants.

Couple things.
j08691 is right, IDs must be unique. The browser is getting the first #count-checked-checkboxes and filling that value.
Also, you are getting the count for all checkboxes which is going to tally all checkboxes regardless of which div they are in
I would suggest the following:
$(document).ready(function() {
var $checkboxes = $(
'.a ul input[type="checkbox"]');
$checkboxes.change(function() {
var countCheckedCheckboxes = $checkboxes.filter(
':checked').length;
$('#count-checked-checkboxes-a').text(
countCheckedCheckboxes);
$('#edit-count-checked-checkboxes-a').val(
countCheckedCheckboxes);
});
});
But you are still getting all checkboxes. At which point I would change your second <div class="a"> to <div class="b"> and then duplicate the above statement like so:
$(document).ready(function() {
var $checkboxes = $(
'.b ul input[type="checkbox"]');
$checkboxes.change(function() {
var countCheckedCheckboxes = $checkboxes.filter(
':checked').length;
$('#count-checked-checkboxes-b').text(
countCheckedCheckboxes);
$('#edit-count-checked-checkboxes-b').val(
countCheckedCheckboxes);
});
});
And of course change your IDs to match the ones in the jquery.
https://jsfiddle.net/gqcgwjq2/28/

Related

Get selected radio button on selected in function in jQuery

I have the following markup:
<div class="secondary-filter" onclick="searchByFilter()">
<ul class="secondary-filter__list">
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-all" name="secondaryFilter" value="All">
<label for="secondaryFilter-all">All</label>
</li>
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-marked" name="secondaryFilter" value="Marked">
<label for="secondaryFilter-quoted">Marked</label>
</li>
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-pending" name="secondaryFilter" value="Pending">
<label for="secondaryFilter-unquoted">Pending</label>
</li>
</ul>
</div>
How can I obtained the label of the checked radio button in the following function?
function searchByFilter() {
var x = $("input[name='secondaryFilter']").find('input:checked');
console.log(x.val());
}
I'm trying this.. but it's not working. Please help.
Your $("input[name='secondaryFilter']") creates a jQuery object if <input> elements, but .find only searches through descendants - the input elements don't have any descendants, rather you want to get the matching <input> in the current collection.
You should also attach the event listener using Javascript rather than in an HTML attribute. By listening for change events, you'll have events that only fire when one of the inputs change, rather than when anywhere in the container is clicked.
Also, probably best to have the label next to each input have its for attribute match the id of the input - that way, when clicking the label, the input will be checked - eg, change
<input type="radio" id="secondaryFilter-marked" name="secondaryFilter" value="Marked">
<label for="secondaryFilter-quoted">Marked</label>
to
<input type="radio" id="secondaryFilter-marked" name="secondaryFilter" value="Marked">
<label for="secondaryFilter-marked">Marked</label>
^^^^^^
While you could use .filter instead, to find the element in the current jQuery object matching the condition:
$("input[name='secondaryFilter']").on('change', function() {
var x = $("input[name='secondaryFilter']").filter('input:checked');
console.log(x.val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="secondary-filter">
<ul class="secondary-filter__list">
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-all" name="secondaryFilter" value="All">
<label for="secondaryFilter-all">All</label>
</li>
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-marked" name="secondaryFilter" value="Marked">
<label for="secondaryFilter-marked">Marked</label>
</li>
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-pending" name="secondaryFilter" value="Pending">
<label for="secondaryFilter-pending">Pending</label>
</li>
</ul>
</div>
It would be easier to just put the :checked into the first selector string:
const checkedInput = $("input[name='secondaryFilter']:checked");
$("input[name='secondaryFilter']").on('change', function() {
var x = $("input[name='secondaryFilter']:checked");
console.log(x.val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="secondary-filter">
<ul class="secondary-filter__list">
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-all" name="secondaryFilter" value="All">
<label for="secondaryFilter-all">All</label>
</li>
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-marked" name="secondaryFilter" value="Marked">
<label for="secondaryFilter-marked">Marked</label>
</li>
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-pending" name="secondaryFilter" value="Pending">
<label for="secondaryFilter-pending">Pending</label>
</li>
</ul>
</div>
To make this work attach a change event handler directly to the radio elements. Then you can get $(this).val() from it when the event occurs. You can also use next().text() to get the value shown in the label, although given that the radio value and innerText of the label are the same, this seems a little redundant.
Also note that the for attributes of the label elements need to match the id of the targeted radio, so I've fixed the HTML there too.
$('.secondary-filter :radio').on('change', function() {
var $radio = $(this);
var $label = $radio.next();
console.log(`value: ${$radio.val()}, label: ${$label.text()}`);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="secondary-filter">
<ul class="secondary-filter__list">
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-all" name="secondaryFilter" value="All">
<label for="secondaryFilter-all">All</label>
</li>
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-marked" name="secondaryFilter" value="Marked">
<label for="secondaryFilter-marked">Marked</label>
</li>
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-pending" name="secondaryFilter" value="Pending">
<label for="secondaryFilter-pending">Pending</label>
</li>
</ul>
</div>
Use
input[name='secondaryFilter']:checked" ,'.secondary-filter'
It will check for checked radio button inside the class of the div
function searchByFilter() {
var x = $("input[name='secondaryFilter']:checked" ,'.secondary-filter')
console.log(x.val())
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="secondary-filter" onclick="searchByFilter()">
<ul class="secondary-filter__list">
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-all" name="secondaryFilter" value="All">
<label for="secondaryFilter-all">All</label>
</li>
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-marked" name="secondaryFilter" value="Marked">
<label for="secondaryFilter-quoted">Marked</label>
</li>
<li class="secondary-filter__list-item">
<input type="radio" id="secondaryFilter-pending" name="secondaryFilter" value="Pending">
<label for="secondaryFilter-unquoted">Pending</label>
</li>
</ul>
</div>
var x = $('input[name=secondaryFilter]:checked').val()

jQuery manipulate DOM

this is my default HTML-Markup from the Wordpress-Plugin:
<ul class="gfield_checkbox" id="input_2_21">
<li class="gchoice_2_21_1">
<input name="input_21.1" type="checkbox" value="Option One" id="choice_2_21_1">
<label for="choice_2_21_1" id="label_2_21_1">Option One</label>
</li>
<li class="gchoice_2_21_2">
<input name="input_21.2" type="checkbox" value="Option Two" id="choice_2_21_2">
<label for="choice_2_21_2" id="label_2_21_2">Option Two</label>
</li>
</ul>
After the document is ready I want to change the HTML-Markup to this:
<ul class="gfield_checkbox" id="input_2_21">
<li class="gchoice_2_21_1 checkbox checkbox-styled">
<label for="choice_2_21_1" id="label_2_21_1">
<input name="input_21.1" type="checkbox" value="Option One" id="choice_2_21_1">
<span>Option One</span>
</label>
</li>
<li class="gchoice_2_21_2 checkbox checkbox-styled">
<label for="choice_2_21_2" id="label_2_21_2">
<input name="input_21.2" type="checkbox" value="Option Two" id="choice_2_21_2">
<span>Option Two</span>
</label>
</li>
</ul>
Thats what I actually did:
$('.gfield_checkbox > li').addClass('checkbox checkbox-styled');
But how do I change the other parts of the code?
To change the html, there is a few ways to do it. One way is to wrap the text inside with the span, and than select the sibling and add it inside of the label.
$("li")
.addClass("checkbox checkbox-styled")
.find("label")
.wrapInner("<span/>")
.each(function(){
label = $(this)
label.prepend(label.prev())
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="gfield_checkbox" id="input_2_21">
<li class="gchoice_2_21_1">
<input name="input_21.1" type="checkbox" value="Option One" id="choice_2_21_1">
<label for="choice_2_21_1" id="label_2_21_1">Option One</label>
</li>
<li class="gchoice_2_21_2">
<input name="input_21.2" type="checkbox" value="Option Two" id="choice_2_21_2">
<label for="choice_2_21_2" id="label_2_21_2">Option Two</label>
</li>
</ul>
Details are commented in demo.
Demo
/*
On each <li>...
- ...add class .checkbox and .checkbox-styled
- Find each <label> and <input> nested in each <li>
- Remove the text from the <label>...
- ...append the <input> to <label>...
- ...append a <span> to <label>...
- ...insert the value of <input> as the text of the <span>
*/
$('li').each(function(i) {
$(this).addClass('checkbox checkbox-styled');
var label = $(this).find('label');
var input = $(this).find('input');
label.text('').append(input).append(`<span>${input.val()}</span>`);
});
<ul class="gfield_checkbox" id="input_2_21">
<li class="gchoice_2_21_1">
<input name="input_21.1" type="checkbox" value="Option One" id="choice_2_21_1">
<label for="choice_2_21_1" id="label_2_21_1">Option One</label>
</li>
<li class="gchoice_2_21_2">
<input name="input_21.2" type="checkbox" value="Option Two" id="choice_2_21_2">
<label for="choice_2_21_2" id="label_2_21_2">Option Two</label>
</li>
</ul>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

how to reuse function in javascript

I wrote a function in javascript. It works perfectly to show and hide list items on click action. I have other 5 lists items that I want to use this function for them also. However, When I tried to generalize the function by passing parameters as IDs it did not work.
the following part of the html:
function toggle_att_menu() {
var att_list = document.getElementById("att-list");
var plus = document.getElementById("plus");
var minus = document.getElementById("minus");
att_list.style.display=
((att_list.style.display!='block')?'block':'none');
plus.style.display=
((plus.style.display!='none')?'none':'inline');
minus.style.display=
((minus.style.display!='inline')?'inline':'none');
}
// what I want to do is passing parameter as following:
function toggle_att_menu(x,y,z) {
var att_list = document.getElementById(x);
var plus = document.getElementById(y);
var minus = document.getElementById(z);
att_list.style.display=
((att_list.style.display!='block')?'block':'none');
plus.style.display=
((plus.style.display!='none')?'none':'inline');
minus.style.display=
((minus.style.display!='inline')?'inline':'none');
}
//and use the function five times with different IDs each time as following:
toggle_att_menu('att-list-1','plus-1','minus-1')
toggle_att_menu('att-list-2','plus-2','minus-2')
toggle_att_menu('att-list-3','plus-3','minus-3')
toggle_att_menu('att-list-4','plus-4','minus-4')
toggle_att_menu('att-list-5','plus-5','minus-5')
<div class="title linklike">
<span id="plus-1" class="plus" >▸</span><span id="minus-1" class="minus">▾</span> <a id="linklike" onclick="toggle_att_menu()" href="#"> condition </a>
</div>
<ul id= "att-list-1" class="att-list">
<li class="checkbox ">
<label>
<input id="condition_1" name="condition" class="multi_checkbox" value="1" type="checkbox"
/>
new
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_2" name="condition" class="multi_checkbox" value="2" type="checkbox"
/>
like new
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_3" name="condition" class="multi_checkbox" value="3" type="checkbox"
/>
excellent
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_4" name="condition" class="multi_checkbox" value="4" type="checkbox"
/>
good
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_5" name="condition" class="multi_checkbox" value="5" type="checkbox"
/>
fair
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_6" name="condition" class="multi_checkbox" value="6" type="checkbox"
/>
damegaed
</label>
</li>
</ul>
</div>
<div class="search-attribute hide-list" data-attr="condition">
<div class="title linklike">
<span id="plus-2" class="plus" >▸</span><span id="minus-2" class="minus">▾</span> <a id="link" onclick="toggle_att_menu()" href="#">condition</a>
</div>
<ul id= "att-list-2" class="att-list">
<li class="checkbox ">
<label>
<input id="condition_1" name="condition" class="multi_checkbox" value="1" type="checkbox"
/>
new
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_2" name="condition" class="multi_checkbox" value="2" type="checkbox"
/>
like new
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_3" name="condition" class="multi_checkbox" value="3" type="checkbox"
/>
excellent
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_4" name="condition" class="multi_checkbox" value="4" type="checkbox"
/>
good
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_5" name="condition" class="multi_checkbox" value="5" type="checkbox"
/>
fair
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_6" name="condition" class="multi_checkbox" value="6" type="checkbox"
/>
damaged
</label>
</li>
</ul>
</div>
any idea or solution will be highly appreciated.I am not so familiar with jQuery. so please I need help using only pure java script.
Try it like this:
https://jsfiddle.net/bertdireins/dtmdod15/6/
function toggle_att_menu(elem) {
var container = elem.parentElement.parentElement;
var att_list = container.querySelector(".att-list");
var plus = container.querySelector(".plus");
var minus = container.querySelector(".minus");
att_list.style.display = ((att_list.style.display!='block')?'block':'none');
plus.style.display =((plus.style.display!='none')?'none':'inline');
minus.style.display = ((minus.style.display!='inline')?'inline':'none');
}
You first have to get the parent parent's container of your anker and then you can query the underlying elements.
Your ankers should then look like this:
<div class="title linklike">
<span id="plus" class="plus">▸</span>
<span id="minus" class="minus">▾</span>
<a id="linklike" onclick="toggle_att_menu(this)" href="#"> condition </a>
</div>
<ul id="att-list" class="att-list"> ... </ul>
Please be aware the fiddle is missing the initial CSS, so it looks little weird.
Query would make life much easier here.
The reason for this is your function calls, and your element IDs. You have no elements with id "att-list-1", "att-list-2" etc. You have two elements with the same id: "att-list". Two elements should never have the same id. Switch the ids to "att-list-1", "att-list-2" and so forth, and it probably should work. The same goes for "plus-1" ... and "minus-1" ..., you have no elements with those IDs in HTML, you use the same IDs "minus" and "plus" for several elements.
You are not using unique IDs for your elements - it's better to use classes for repeated elements. Anyway, you probably want something like that:
/*
function toggle_att_menu(x,y,z) {
var att_list = document.getElementById(x);
var plus = document.getElementById(y);
var minus = document.getElementById(z);
att_list.style.display = ((att_list.style.display!='block')?'block':'none');
plus.style.display = ((plus.style.display!='none')?'none':'inline');
minus.style.display = ((minus.style.display!='inline')?'inline':'none');
}
*/
function toggle_att_menu(element) {
var parent = element.parentElement.parentElement;
var att_list = parent.querySelector(".att-list");
var plus = parent.querySelector(".plus");
var minus = parent.querySelector(".minus");
att_list.style.display = ((att_list.style.display!='block')?'block':'none');
plus.style.display = ((plus.style.display!='none')?'none':'inline');
minus.style.display = ((minus.style.display!='inline')?'inline':'none');
}
<div class="search-attribute hide-list" data-attr="condition">
<div class="title linklike">
<span id="plus-1" class="plus">▸</span><span id="minus-1" class="minus">▾</span><a id="link-1" onclick="toggle_att_menu(this)" href="#"> condition </a>
</div>
<ul id="att-list-1" class="att-list">
<li class="checkbox ">
<label>
<input id="condition_1_1" name="condition" class="multi_checkbox" value="1" type="checkbox"/>
new
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_1_2" name="condition" class="multi_checkbox" value="2" type="checkbox"/>
like new
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_1_3" name="condition" class="multi_checkbox" value="3" type="checkbox"/>
excellent
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_1_4" name="condition" class="multi_checkbox" value="4" type="checkbox"/>
good
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_1_5" name="condition" class="multi_checkbox" value="5" type="checkbox"/>
fair
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_1_6" name="condition" class="multi_checkbox" value="6" type="checkbox"/>
damegaed
</label>
</li>
</ul>
</div>
<div class="search-attribute hide-list" data-attr="condition">
<div class="title linklike">
<span id="plus-2" class="plus" >▸</span><span id="minus-2" class="minus">▾</span> <a id="link-2" onclick="toggle_att_menu(this)" href="#">condition</a>
</div>
<ul id="att-list-2" class="att-list">
<li class="checkbox ">
<label>
<input id="condition_2_1" name="condition" class="multi_checkbox" value="1" type="checkbox"/>
new
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_2_2" name="condition" class="multi_checkbox" value="2" type="checkbox"/>
like new
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_2_3" name="condition" class="multi_checkbox" value="3" type="checkbox"/>
excellent
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_2_4" name="condition" class="multi_checkbox" value="4" type="checkbox"/>
good
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_2_5" name="condition" class="multi_checkbox" value="5" type="checkbox"/>
fair
</label>
</li>
<li class="checkbox ">
<label>
<input id="condition_2_6" name="condition" class="multi_checkbox" value="6" type="checkbox"/>
damaged
</label>
</li>
</ul>
</div>
As a general piece of advice, try:
not using the same id multiple times, because that's not how the id is supposed to work. Ids ought to be unique, otherwise, you're going to have problems with CSS and JavaScript in particular.
getting your elements by class, or some other non-unique selector, instead of id, using a function that can do that, such as getElementsByClassName orquerySelectorAll so that you can use your function just once.
using window.getComputedStyle(el).getPropertyValue as a fallback, when getting the current display to check against, in case the style is not defined yet (if you don't the first click will do nothing).
Now, regarding your problem, since you can't use jQuery, getting the elements will get kind of ugly and will depend vastly on how you position them relatively to each other. Since, you are using inline JavaScript to call your function, be sure to pass this as an argument to be able to use the link to get the required elements.
Snippet:
/* ----- JavaScript ----- */
function toggle_att_menu(a) {
var
/* Cache all desired elements. */
elements = {
att_list: a.parentElement.nextElementSibling.children[0],
plus: a.previousElementSibling.previousElementSibling,
minus: a.previousElementSibling
},
/* Store the wanted displays for each element type. */
displays = {
att_list: {from: "block", to: "none"},
plus: {from: "inline", to: "none"},
minus: {from: "none", to: "inline"}
};
/* Iterate over every property of the elements object. */
for (var type in elements) {
var
/* Cache the current element. */
element = elements[type],
/* Get the current display of the element. */
d = element.style.display ||
getComputedStyle(element, null).getPropertyValue("display");
/* Set the element's display to the appropriate one. */
element.style.display = (d != displays[type].from)
? displays[type].from
: displays[type].to;
}
}
/* ----- CSS ----- */
.plus { display: inline }
.minus { display: none }
<!----- HTML ----->
<div class="title linklike">
<span class="plus">▸</span>
<span class="minus">▾</span>
<a class="linklike" onclick="toggle_att_menu(this)" href="#">condition</a>
</div>
<div class="search-attribute hide-list" data-attr="condition">
<ul class="att-list">
<li class="checkbox">
<label>
<input name="condition" class="multi_checkbox" value="1" type="checkbox"/>
new
</label>
</li>
<li class="checkbox">
<label>
<input name="condition" class="multi_checkbox" value="2" type="checkbox"/>
like new
</label>
</li>
<li class="checkbox">
<label>
<input name="condition" class="multi_checkbox" value="3" type="checkbox"/>
excellent
</label>
</li>
<li class="checkbox">
<label>
<input name="condition" class="multi_checkbox" value="4" type="checkbox"/>
good
</label>
</li>
<li class="checkbox">
<label>
<input name="condition" class="multi_checkbox" value="5" type="checkbox"/>
fair
</label>
</li>
<li class="checkbox">
<label>
<input name="condition" class="multi_checkbox" value="6" type="checkbox"/>
damaged
</label>
</li>
</ul>
</div>
<div class="title linklike">
<span class="plus">▸</span>
<span class="minus">▾</span>
<a class="linklike" onclick="toggle_att_menu(this)" href="#">condition</a>
</div>
<div class="search-attribute hide-list" data-attr="condition">
<ul class="att-list">
<li class="checkbox">
<label>
<input name="condition" class="multi_checkbox" value="1" type="checkbox"/>
new
</label>
</li>
<li class="checkbox">
<label>
<input name="condition" class="multi_checkbox" value="2" type="checkbox"/>
like new
</label>
</li>
<li class="checkbox">
<label>
<input name="condition" class="multi_checkbox" value="3" type="checkbox"/>
excellent
</label>
</li>
<li class="checkbox">
<label>
<input name="condition" class="multi_checkbox" value="4" type="checkbox"/>
good
</label>
</li>
<li class="checkbox">
<label>
<input name="condition" class="multi_checkbox" value="5" type="checkbox"/>
fair
</label>
</li>
<li class="checkbox">
<label>
<input name="condition" class="multi_checkbox" value="6" type="checkbox"/>
damaged
</label>
</li>
</ul>
</div>

How can I add a javascript alert message if checkbox is checked more than one?

My code is like this:
<div class="col-md-4">
<ul class="list-unstyled">
<li><strong>England</strong></li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Chelsea
</label>
</div>
</li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Mu
</label>
</div>
</li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Arsenal
</label>
</div>
</li>
</ul>
</div>
<div class="col-md-4">
<ul class="list-unstyled">
<li><strong>Spain</strong></li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Madrid
</label>
</div>
</li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Barcelona
</label>
</div>
</li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Atletico
</label>
</div>
</li>
</ul>
</div>
jsfiddle: https://jsfiddle.net/oscar11/m7by6zcw/
In each checkbox group, the user can only select a maximum of 1. If the user selects more than 1, I want to display alert messages.
For example in the England checkbox group, if the user chooses Chelsea, then chooses Arsenal again, it must display an alert message.
How can I do it?
I tried it. But I'm still confused
Try this
$('input[type="checkbox"]').change(function(){
var group = $("ul.list-unstyled");
for(var i=0;i<group.length;i++){
if($("input:checked",group[i] ).length>1){
alert("Not allowed to select more than one");
this.checked = false;
return;
}
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-4">
<ul class="list-unstyled">
<li><strong>England</strong></li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Chelsea
</label>
</div>
</li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Mu
</label>
</div>
</li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Arsenal
</label>
</div>
</li>
</ul>
</div>
<div class="col-md-4">
<ul class="list-unstyled">
<li><strong>Spain</strong></li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Madrid
</label>
</div>
</li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Barcelona
</label>
</div>
</li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Atletico
</label>
</div>
</li>
</ul>
</div>
I think a better option is to style radio buttons to look like checkboxes:
Very nice example I googled: http://cssdeck.com/labs/ldmtsmfk
So you don't need to struggle with JS logic which change the default behavior of radio buttons.
Without jQuery.
var checkboxes = document.querySelectorAll('[type="checkbox"]');
for (var ix = 0, length = checkboxes.length; ix < length; ix++) {
checkboxes[ix].addEventListener('change', function() {
if (document.querySelectorAll(':checked').length > 1) {
alert('no');
this.checked = false;
}
});
}
<div class="col-md-4">
<ul class="list-unstyled">
<li><strong>England</strong></li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Chelsea
</label>
</div>
</li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Mu
</label>
</div>
</li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Arsenal
</label>
</div>
</li>
</ul>
</div>
<div class="col-md-4">
<ul class="list-unstyled">
<li><strong>Spain</strong></li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Madrid
</label>
</div>
</li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Barcelona
</label>
</div>
</li>
<li>
<div class="checkbox">
<label>
<input type="checkbox"> Atletico
</label>
</div>
</li>
</ul>
</div>

How to show selected value of type="radio" on populate?

On clicking on the Populate Values button the div below the button should populate the values of the selected radio buttons of the preferred and home locations respectively
I want to do it unobtrusively without inline JavaScript
<div id="form_block">
<div class="home-location">
<ul>
<li>Home Location</li>
<li>
<input type="radio" name="home-location" value="india" class="radio" id="home-india" /><label
for="home-india">India</label></li>
<li>
<input type="radio" name="home-location" value="usa" class="radio" id="home-usa" /><label
for="home-usa">USA</label></li>
</ul>
</div>
<div class="prefer-location">
<ul>
<li>Preferred Location</li>
<li>
<input type="radio" name="home-preferred" value="india" class="radio" id="preferred-india" /><label
for="preferred-india">India</label></li>
<li>
<input type="radio" name="home-preferred" value="usa" class="radio" id="preferred-usa" /><label
for="preferred-usa">USA</label></li>
</ul>
</div>
<div class="result">
My Home Location is: <span class="home-location-result"></span>and my preferred
location is: <span class="home-location-result"></span>
</div>
<input type="submit" value="Populate Values" id="ss" class="submit" />
Modified your HTML. Provided an id attribute for your result spans
$(function(){
$("#ss").click(function(){
// Find all input elements with type radio which are descendants of element with id `form`
var filt = $("#form_block input:radio");
// Apply a filter to the above object with `name='home-location'` and checked and get the value
var homeVal = filt.filter("[name='home-location']:checked").val();
// same as above
var preferredVal = filt.filter("[name='home-preferred']:checked").val();
// Set the text of the element width id `home-location-result' with the computed value
$("#home-location-result").text(homeVal);
// same as above
$("#preferred-location-result").text(preferredVal);
return false;
});
});
See a working demo
use this it's works for me
<script>
function test()
{
var home= $('input:radio[name=home-location]:checked').val();
document.getElementById("h1").innerHTML=home;
var preferred= $('input:radio[name=home-preferred]:checked').val();
document.getElementById("h2").innerHTML=preferred;
return false;
}
</script>
<form onsubmit="return test();">
<div id="form_block">
<div class="home-location">
<ul>
<li>Home Location</li>
<li>
<input type="radio" name="home-location" value="india" class="radio" id="home-india" /><label
for="home-india">India</label></li>
<li>
<input type="radio" name="home-location" value="usa" class="radio" id="home-usa" /><label
for="home-usa">USA</label></li>
</ul>
</div>
<div class="prefer-location">
<ul>
<li>Preferred Location</li>
<li>
<input type="radio" name="home-preferred" value="india" class="radio" id="preferred-india" /><label
for="preferred-india">India</label></li>
<li>
<input type="radio" name="home-preferred" value="usa" class="radio" id="preferred-usa" /><label
for="preferred-usa">USA</label></li>
</ul>
</div>
<div class="result">
My Home Location is: <span class="home-location-result" id="h1"></span>and my preferred
location is: <span class="home-location-result" id='h2'></span>
</div>
<input type="submit" value="Populate Values" id="ss" class="submit" />
</form>

Categories

Resources