I made a small verification script which is supposed to act like this:
I have 4 checkboxes, one has a particular way of action, the id of this checkbox is chx0
If I checked the chx0 checkbox then it released all the others checked checkboxes
If I checked one of all the others checkboxes then it released the chx0 one
Here is my code:
<script type="text/javascript">
$(document).ready(function() {
$('#chx0').click(function() { // click on the chx0 checkbox
if ($('#chx0').attr('checked')) {
$('#chx1').attr('checked', false);
$('#chx2').attr('checked', false);
$('#chx3').attr('checked', false);
}
});
$('#chx1').click(function() {// click on the chx1 checkbox
if ($('#chx1').attr('checked')) {
$('#chx0').attr('checked', false);
}
});
$('#chx2').click(function() { // click on the chx2 checkbox
if ($('#chx2').attr('checked')) {
$('#chx0').attr('checked', false);
}
});
$('#chx3').click(function() { // click on the chx3 checkbox
if ($('#chx3').attr('checked')) {
$('#chx0').attr('checked', false);
}
});
});
</script>
This code is working pretty well it just to get more good practice!
I'd put a class on each checkbox
<input type="checkbox" name="chx0" id="chx0" class="checkbox-group singleton">
<label for="chx0">Check me out!</label>
<input type="checkbox" name="chx1" id="chx1" class="checkbox-group">
<label for="chx1">Check us out!</label>
<input type="checkbox" name="chx2" id="chx2" class="checkbox-group">
<label for="chx2">Check them out!</label>
Then with your jQuery you can do
$('input.checkbox-group').each( function() {
$(this).click( function() {
if ( $(this).hasClass('singleton') ) {
$('input.checkbox-group:checked').removeAttr('checked');
} else {
$('input.checkbox-group.singleton').removeAttr('checked');
}
};
});
Untested, but I think something like that should work. I can't remember if it's better to use the change event rather than click.
You could combine the clauses for all the individual checkboxes, something like
$('#chx1','#chx2','#chx3').click(function() {
if ($(this).attr('checked')) {
$('#chx0').attr('checked', false);
}
});
instead of one per checkbox. Also in the one for "chx0", you can use $(this) instead of $('#chx0')
I came up with something similar to ianbarker. Rather than assume dependencies are all or nothing I used a custom data tag to list the dependencies.
A working example is on jsfiddle here
$('.luckyChecks').click(function() { // click on ANY lucky checkbox
var $t = $(this);
if($t.attr('checked')){
var clear = $t.attr("data-clear").split(",");
for(var i=0; i < clear.length; i++){
$('#'+clear[i]).attr('checked',false);
}
}
});
HTML:
<input type="checkbox" id="chx0" class="luckyChecks" data-clear="chx1,chx2,chx3" />
Diamonds <br />
<input type="checkbox" id="chx1" class="luckyChecks" data-clear="chx0"/>
Clovers <br />
...
Related
Hi i have got a checkbox and when i click it and check the box a function runs, which works just how i want it to.. now i want to run a DIFFERENT function if it is checked off but it is just running the same function everytime.
<input type="checkbox" class="no-custom" onclick="CheckBox()">
function CheckBox() {
$("#emailMain").css({"display": "none"});
$("#emailSame").css({"display": "inline"});
var Mainemail = Customer().email['#text']();
Contact().email = Mainemail;
EmailHolder(Mainemail);
}
Any ideas in the best way to sort this?
Firstly, if you've included jQuery in your page you should use it to attach your events as its a better separation of concerns. You can then use the checked property of the element to determine which function to call:
<input type="checkbox" class="no-custom" />
$('.no-custom').change(function() {
if (this.checked) {
// do something...
}
else {
// do something else...
}
});
Add an ID to the checkbox:
<input type="checkbox" id="the-checkbox" class="no-custom" onclick="CheckBox()">
Then add an event listener for when it changes:
$(function() {
$('#the-checkbox').change(function() {
if($(this).is(':checked')) {
oneFunction();
}
else {
anotherFunction();
}
});
function oneFunction() {
}
function anotherFunction() {
}
});
You can easily check if the checkbox is checked using jQuery is.
<input type="checkbox" class="no-custom" onclick="CheckBox(this)">
function CheckBox(cb) {
if ($(cb).is(":checked")) {
alert("Checked");
CheckedFunction();
} else {
alert("Unchecked");
UncheckedFunction
}
}
So first off I did search and I found this Enable/Disable a dropdownbox in jquery which got me on the right track. I'm new to jquery so when seeing other code I can adapt it to fit and work for me.
So what i'm asking is how do you make it check to see if two check boxes condition are true?
<script>
$(document).ready(function() {
$("#box1").click(function() {
if ($(this).is(":checked")) {
$("#tech1").prop("disabled", false);
} else {
$("#tech1").prop("disabled", true);
}
});
});
</script>
I want it to be if box1 and box2 are checked enable this box? I understand you can do it with an if statement, but I'm not sure where exactly it goes. Thanks in advance.
Would this work:
$("#box1").click(function() {
if ($(this).is(":checked")) &&
$("#box2").click(function() {
if ($(this).is(":checked"))
I doesn't work and I assume thats because that above creates two functions and not the if statement that I need.
Include both in the event handler, and check if both are checked
$(document).ready(function() {
var boxes = $("#box1, #box2");
boxes.on('change', function() {
var disabled = boxes.filter(':checked').length === boxes.length;
$("#tech1").prop("disabled", disabled);
});
});
FIDDLE
Consider box1 & box2 checkboxes defined with a css class boxes as below:
<input type="checkbox" id="box1" class="boxes" />
<input type="checkbox" id="box2" class="boxes" />
Now you can use box class as jquery selector to do your task
<script>
$(document).ready(function() {
$(".boxes").click(function(){
if($(".boxes:checked").size() == $(".boxes").size()){
$("#tech1").prop("disabled", false);
}else{
$("#tech1").prop("disabled", true);
}
);
});
I have group of checkboxes and that are compulsory to be applied but the situation is user can be able to check only one check box at a time. So, for this I have implemented something like this with the help of internet. No doubt it works fine when there are no checkbox checked by default. But suppose, one of the checkbox is checked true when page loads, then this does not works unitl I click on checkbox twice.
Here is what I am using::
So , Assuming I have set of 5 checkboxes, I set same class name for all the checkboxes and then
<input type="checkbox" class="myclass" onclick="Checkme(this.className);"/>
<input type="checkbox" class="myclass" onclick="Checkme(this.className);"/>
<input type="checkbox" class="myclass" onclick="Checkme(this.className);"/>
<input type="checkbox" class="myclass" onclick="Checkme(this.className);"/>
<input type="checkbox" class="myclass" onclick="Checkme(this.className);"/>
In View page I have declared::
function Checkme(class_Name) {
Check_OR_Uncheck(class_Name);
}
In Common js::
function Check_OR_Uncheck(class_Name) {
$("." + class_Name).click(function () {
if ($(this)[0].checked) {
$("." + class_Name).each(function () {
$(this)[0].checked = false;
});
$(this)[0].checked = true;
}
else {
$(this)[0].checked = false;
}
});
}
Please Help me to achieve this..
Keep your code in the document ready event. This will register the click event for "myclass".
$(".myclass").click(function () {
if ($(this)[0].checked) {
$(".myclass").each(function () {
$(this)[0].checked = false;
});
$(this)[0].checked = true;
} else {
$(this)[0].checked = false;
}
});
jsfiddle
You could use document ready handler and call method:
jsFiddle
$(function(){
$(':checkbox:checked').each(function(){
Checkme(this.className);
});
});
Try this
$(function(){
$('.myclass').click(function(){
var s=$(this).prop('checked');
if(s==true)
{
$('.myclass').prop('checked',false)
$(this).prop('checked',true)
}
});
});
Or
You simply can use
if(s==true)
{
$(this).siblings().prop('checked',false);
}
FIDDLE
Try this
$(function(){
$('input:checkbox').prop('checked', false)
$('input:checkbox').click(function(){
if($(this).is(':checked')){
$('input:checkbox').not(this).prop('checked', false);
}
});
})
Instead of implementing a group of check boxes that behave like a group of radio buttons, I suggest implementing a group of radio buttons that look like a group of check boxes:
input[type=radio] {content:url(mycheckbox.png)}
input[type=radio]:checked {content:url(mycheckbox-checked.png)}
This approach simplifies your implementation; you have two one-line CSS rules instead of a JS event handler function, event binding (on both document ready and the HTML element itself), not to mention a possible dependency on jQuery (if you choose to use it).
The catch to this approach is that it requires CSS3 support. For more info, check out this SO answer: https://stackoverflow.com/a/279510/2503516
I am trying to check if a radio box is checked using JavaScript, but I can't seem to figure it out properly.
This is the HTML code:
<input type="radio" name="status" id="employed_yes" value="yes">
<input type="radio" name="status" id="employed_no" value="no">
I have tried using jQuery as follows:
$(document).ready(function() {
if($('#employed_yes').is(':checked')) {
// do something
}
});
Also, I tried using pure Javascript by getting the element and check its 'checked' attribute, but it didn't work.
I look forward to your insight!
Thank you!
Use onchange
$('input[type="radio"]').on('change',function(){
if($('#employed_yes').is(':checked')) {
alert("yes");
}
});
DEMO
Try to check using name of the radio buttons like
if($('input[name="status"]').val() != "") {
// do something
} else {
alert("Select an Status");
}
Your solution doesn't work because when the page loads the checkbox's default state is unchecked, which is when the jQuery code runs.
You need to listen for the change event on the checkbox like so:
$(document).ready(function() {
$("#employed_yes").change(function() {
if(this.checked) {
document.write("checked");
}
});
});
Demo: http://jsfiddle.net/7CduY/
Try this. This will alert Hi on document ready if the any radio button is checked. If you want to check on specific event then you can bind on any event to check same.
$(document).ready(function() {
if($('input[type=radio]').is(':checked')) {
alert("Hi");
}
});
if($('input:radio:checked').text("yes") {
// do something
}
Got the idea from the jQuery forum. http://forum.jquery.com/topic/how-to-check-whether-all-radio-buttons-have-been-been-selected
Dude, I think you Wanted, whether radio button is checked or not, this what i understand from your question
If so here it is
$(document).ready(function() {
$('input[type="radio"]').on('change',function(){
if($('[name=status]:checked').length) {
alert("checked");
}
});
});
FIDDLE DEMO
Pure JS:
<input type = "button" value = "What?" name = "wic" onclick = "whatischecked(this.name);" />
Event onClick:
function whatischecked(name) {
var
emp = document.getElementById("employed_yes").checked
nonemp = document.getElementById("employed_no").checked
if (emp) {
alert("Employed");
};
if (nonemp) {
alert("Non-Employed");
};
if ((emp == false) & (nonemp == false))
{
alert("nothing checked")
};
}
I have the following set of checkboxes:
Original:
<INPUT TYPE="checkbox" ID="db1" class="db" checked>
<INPUT TYPE="checkbox" ID="db2" class="db" checked>
<INPUT TYPE="checkbox" ID="db3" class="db" checked>
<INPUT TYPE="checkbox" ID="db4" class="db" checked>
</br>
Other:
<INPUT TYPE="checkbox" ID="other" class="other" onclick="otherBoxes('other',this)">
and the following javascript:
<script language="JavaScript" src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
function otherBoxes(it,box)
{
$(function()
{
$(':checkbox').click(function()
{
if (this.checked)
{
$('.db').prop('checked',false);
}
}
)
}
)
}
</script>
What I am trying to do is set someting up so that when I check the 'other' checkbox, the 'Original' checkboxes are all unchecked.
I then want to have the reverse, so that if one (or more) of the 'Original' checkboxes are checked, the 'other' checkbox is unchecked.
The Javascript I have so far kind of does the first part of this, in that if I check, then uncheck, then check the 'other' box again, the 'Original' boxes are unchecked.
However, I would like it to work when the box is checked the first time.
It also has the unintended consequence, that after the 'other' box has been checked, the 'original' boxes refuse to be checked, even if I uncheck the 'other' box.
I've found lots of examples of similar situations, but none the same, and I haven't been able to adapt any that I have found. How can I do this please?
You can try something like that
$('.other').on('click' , function() {
$('.db').each(function(){
$(this).removeAttr('checked');
})
});
$('.db').on('click', function(){
$('.other').removeAttr('checked');
});
here it is a working jsfiddle http://jsfiddle.net/vQTFm/
P.S. : I suggest you to avoid using similar names beetwen ids and classs because it CAN be confusing.
You should bind the events using jQuery instead of in the HTML.
Here is some code that does what you want, it binds to the change event on the checkboxes and then checks whether it was the other or db checkboxes that were checked and unchecks the required check-boxes:
$(function() {
$('.db, #other').on('change', function() {
if (this.checked) {
if ($(this).is('#other')) {
$('input:checkbox').not('#other').prop('checked', false);
} else {
$('#other').prop('checked', false);
}
}
});
});
Working example - http://jsfiddle.net/YD5SE/2/
Use the following JS code:
<script type="text/javascript">
function otherBoxes(it,box)
{
if (box.checked)
{
$('.db').prop('checked',false);
}
}
</script>