How to Clear checkbox When checked other checbox ? using java script [duplicate] - javascript

This question already has answers here:
How to clear all checkbox When checkbox id="checkAll" are checked?
(4 answers)
Closed 8 years ago.
How to Clear checkbox When checked other checbox ? using java script
When user checked checkbox id="checkItem1" or id="checkItem2" or id="checkItem3" i want to clear checkbox id="checkAll" How can i do that ? using java script
<input type="checkbox" id="checkAll" checked > Check All
<hr />
<input type="checkbox" id="checkItem1"> Item 1
<input type="checkbox" id="checkItem2"> Item 2
<input type="checkbox" id="checkItem3"> Item 3
http://jsfiddle.net/peap/sydzL8Lc/8/

Using jQuery, you can just write:
$("#checkItem1").click(function() {
$("#checkAll").prop('checked', false);
});
and repeat that for checkItem2 and checkItem3. But I recommend to add a class to all checkItems, that would make it easier to select them.

Related

How to get dynamic array value using check box in angular.Js? [duplicate]

This question already has answers here:
How to bind dynamic Check boxes value using ng-model?
(3 answers)
Closed 5 years ago.
I am trying to get value of check box using angular =. I have java object at HTML page. As user checked individual box so singe value should be in my array, if user check select all so all box should be checked and array should be contain all checked boxes value, and it should be unchecked when select all is unchecked. Here is the code please suggest me some solution.
<input type="checkbox" value="Select All">
<label>
<c:forEach items="${sellerProductsDto.sellerProductColorsDtos}" var="colors">
<input type="checkbox" value="${colors.keyId}" ng-model=""> ${colors.colorName}
</c:forEach>
</label>
This might help. It's providing directive for checkboxlist
https://vitalets.github.io/checklist-model/
Check demo
http://jsfiddle.net/yasirzuberi/r1Lc3ba0/4/

Checkbox with ajax issue for select or unselect issue [duplicate]

This question already has answers here:
jquery attr('checked','checked') works only once
(2 answers)
Closed 6 years ago.
I have checkbox code in html below format
<label class="checkbox-inline">
<input type="checkbox" name="category[]" value="1" data-id="1" class="categorychk" checked="checked">Men
</label>
<label class="checkbox-inline">
<input type="checkbox" name="category[]" value="2" data-id="2" class="categorychk" checked="checked">Women
</label>
On ajax success response I doing following jquery code it will work fine.
$.each(data, function(index,value) {
$(".categorychk[data-id="+value+"]").attr("checked",true);
});
But issue is before call I clear all checked marks and then apply above code. But it does not work.
When I add following code after ajax call
$(".categorychk").each(function(){
$(this).removeAttr("checked");
});
Note : If apply above code without clear checked marks then working fine with the previous checked checkbox.
Ok. You're trying to do stuff with attributes, when you should be using properties. Read http://api.jquery.com/attr/, specifically the section entitled "attributes vs properties".
In the meantime, your code will work if you change
$(".categorychk").each(function(){
$(this).removeAttr("checked");
});
to
$(".categorychk").prop("checked", false);
(N.B. you don't need to .each() this, it will operate on all the selected items automatically).
and change
$(".categorychk[data-id="+value+"]").attr("checked",true);
to
$(".categorychk[data-id="+value+"]").prop("checked",true);
Note the use of .prop() instead of .attr()
As documented in how do i check uncheck a checkbox input or radio button you need to use jQuery prop:
$(".categorychk[data-id="+value+"]").prop('checked', true);
And to filter elements by data-id you can use:
$(".categorychk").filter(function() {
return $(this).data('id') == value;
});

Checkbox in <a href> tag [duplicate]

This question already has answers here:
Check/Uncheck checkbox with JavaScript
(12 answers)
Closed 7 years ago.
This is my HTML code:
<a id="id_1" name="n_1" href="#" onclick="callFn(this.id);">
<input type="checkbox" name="ck_1" id="xyz">
Click Here
</a>
I want to check this checkbox using Javascript code. How to do it??
Use <label> instead of <a>.
<label id="id_1" onclick="callFn(this.id);">
<input type="checkbox" name="ck_1" id="xyz">
Click Here
</labek>
This is the use of <label> tag. And moreover, you don't need callFn(this.id); at all.
Here is simple DEMO.. hope it helps
$("#check").click(function() {
var checkBoxes = $("#xyz");
checkBoxes.attr("checked", !checkBoxes.attr("checked"));
});
<label id="id_1" onclick="callFn(this.id);" for="xyz">
<input type="checkbox" name="ck_1" id="xyz">
Click Here
</labek>
<br/>
<button id="check">By button</button>
Get the element using its ID:document.getElementById("xyz")
Then change its property checked to true:
document.getElementById("xyz").checked = true;

Find out if ASP.NET radio button is checked without knowing the Id using jQuery [duplicate]

This question already has answers here:
Getting ID from asp.net runat server in jQuery
(8 answers)
Closed 7 years ago.
I have an ASP.NET Forms page and there is a fieldset and inside this there are items that each have a radio button. Basically I want to determine if a certain item is checked. However, it appears that it's location could move and therefore the id could change if the database is updated. How can I determine if the item is checked using if the id attribute can change? I don't think the value would change, how can I determine if it is checked by using the value attribute?
I would like to add a class as suggested but I really only want to use javascript and not change any of the ASP.NET code in the .aspx file.
<fieldset style="padding-left: 5px;">
<input id="ctl00_ctl00_MainContent_ContentPlaceHolder_rblstRole_8" type="radio" name="ctl00$ctl00$MainContent$ContentPlaceHolder$rblstRole" value="36">
<label for="ctl00_ctl00_MainContent_ContentPlaceHolder_rblstRole_8">Agency VP</label>
<br>
<input id="ctl00_ctl00_MainContent_ContentPlaceHolder_rblstRole_9" type="radio" name="ctl00$ctl00$MainContent$ContentPlaceHolder$rblstRole" value="13">
<label for="ctl00_ctl00_MainContent_ContentPlaceHolder_rblstRole_9">Agent</label>
...
You can use attribute selector of jQuery:
$('input[value="36"]').is(':checked')
and you can see the working demo here: http://jsfiddle.net/5637csyz/

Check if all checkboxes are checked on submit [duplicate]

This question already has answers here:
Check if checkbox is checked with jQuery
(26 answers)
Closed 8 years ago.
I have this:
<fieldset id="booom">
<label><INPUT TYPE="checkbox" NAME="a" VALUE="a">something></label>
<label><INPUT TYPE="checkbox" NAME="b" VALUE="b">something></label>
<label><INPUT TYPE="checkbox" NAME="c" VALUE="c">something></label>
</fieldset>
<input type="button" id="answer" value="submit">
How can I check with jQuery if all checkboxes from fieldset 'booom' are unchecked on submit?
So:
if (all checkboxes from parent 'booom' are unchecked) {
alert("something")
}
I found many posts about this but none with an actual simple working sample with a button and an alert box :)
Thanks!
You can use the :checked modifier in a selector.
if ($("#boom :checkbox:checked").length == 0) {
alert("You have to check at least one box");
}

Categories