var cbx = document.getElementById('ModelFilter').getElementsByTagName('input');
var ArrCB_l=cbx.length;
while(ArrCB_l--){
var CB=ArrCB[ArrCB_l];
CB.checked()==True;
return 1
}
return 0
Can anyone tell me what is wrong with this? I know the first line is the correct input as I am using it elsewhere for the same checkboxes, however this won't work? I am trying to make sure there is at least one checkbox checked. This function is called with an onsubmit event.
while(ArrCB_1) {
if(cbx[--ArrCB_1].checked) {
return true;
}
}
return false;
.checked() should be just .checked
while(ArrCB_l--){
if (cbx[ArrCB_l].checked) {
return true;
}
}
return false;
your collection is cbx and not ArrCB, and checked is not a method but it's an attribute/property of the element
document.getElementById('ModelFilter').getElementsByTagName('input');
this code returns all inputs(text,button,radio etc)
use
document.getElementById('ModelFilter').getElementsByTagName("input")[ArrCB_l].type == "checkbox"
Related
I've tried searching the site, but am really struggling to find what I want... Basically I have some jQuery code that checks the state of three IDs, they are tied to three checkboxes;
$('#submitButton').click(function(){
if($("#cb1,#cb2,#cb3").is(':checked'))
return true;
else
return false;
});
How would I restructure this jQuery statement to make it so that all three checkboxes have to be CHECKED? At the moment, either one can be checked for the action to be performed
I'm betting there is a really simple solution to all of this, but I have been lpooking at it for so long, I just can't see it. Could someone with a fresh pair of eyes and a less addled brain please steer me in the right direction?
You need to use:
$('#submitButton').click(function(){
if ($('#cb1').is(':checked') && $('#cb1').is(':checked') && $('#cb3').is(':checked')) {
return true;
} else {
return false;
}
});
Use
if($("#cb1").is(':checked') && $("#cb2").is(':checked') && $("#cb3").is(':checked'))
$("#cb1,#cb2,#cb3").is(':checked') will return result for the 1st element only
Try:
$('#submitButton').click(function(){
if($("#cb1:checked,#cb2:checked,#cb3:checked").length === 3)
return true;
else
return false;
});
You can use:
if ($('#cb1:checked,#cb2:checked,#cb3:checked').length == 3) {
//all three are checked.do something
}
or
if ($('#cb1:checked,#cb2:checked,#cb3:checked').length == $('#cb1,#cb2,#cb3').length) {
//all three are checked.do something
}
You could try:
$('#submitButton').click(function(){
if($('#cb1')[0].checked && $('#cb2')[0].checked && $('#cb3')[0].checked)return true;
return false;
});
You can just add a class to your checkboxes and use like so:
$('#submitButton').click(function(){
if($(".myCheckbox:checked").length == 3) {
console.log('true');
}
else {
console.log('false');
}
});
Here is the JSFiddle Example:
http://jsfiddle.net/cLH8s/1/
I have two inputs where I am checking to make sure that they are not empty before the form submits.
My issue is that it only validates #from_date. Is the issue that .val will only check the last id in the list?
$('#submitDates').click(function () {
// Get the fields you want to validate
var name = $("#to_date, #from_date");
// Check if field is empty or not
if (name.val()=='') {
alert ('Please Select Dates')
return false;
} ;
});
});
Any specific reason you're hooking on .click and not .submit?
You can iterate through the selected elements and check for a violating element using .each
var found = false;
$("#to_date, #from_date").each(function(i,name){
// Check if field is empty or not
if (!found && $(name).val()=='') {
alert ('Please Select Dates')
found = true;
} ;
});
return !found;
In your example var name = $("#to_date, #from_date"); is giving you a collection of two inputs and by doing if (name.val()=='') jQuery is checking only the first element in the collection, so it's not working. You may try this
$('#submitDates').click(function () {
var name = $("#to_date, #from_date");
if ( name[0].value == '' || name[1].value == '' ) {
alert ('Please Select Dates');
return false;
}
});
In the above example name[0].value refers to the first element and name[1].value refers to the second element. If you want to use jQuery's val() method then you can use it like $(name[0]).val() and $(name[1]).val().
Also you should consider to use submit event of the form instead of button's click event.
Given the following markup:
<input name="active" type="hidden" value="0" />
<input id="active" name="active" type="checkbox" value="1" />
When the checkbox is unchecked and the form is submitted the server will get a value of "0" for the "active" param. When the checkbox is checked and the form is submitted the server will get a value of "1" for the "active" param. This works just fine.
What I want to do is capture the proper value in JavaScript based upon that. The trick, however, is I don't know if the input is a checkbox or not. As far as my script is concerned it is just acting on a set of inputs.
I have created a JSFiddle here: http://jsfiddle.net/bcardarella/5QRjF/ that demonstrates the issue.
TL;DR I want to ensure the value I capture from each input is the actual value sent to the server.
Don't know if you actually want to check for the checkbox or not, but this code works:
$(function() {
var getCheckBoxValue = function() {
if ($('[name="active"]:checkbox').attr("checked")) {
return $('[name="active"]:checkbox').val();
} else {
return $('[name="active"]').val();
}
}
var result = $('#result');
result.append($('<p/>', {text: 'Expected value 0, got: ' + getCheckBoxValue()}));
$(':checkbox')[0].checked = true;
result.append($('<p/>', {text: 'Expected value 1, got: ' + getCheckBoxValue()}));
});
Basically if the checkbox is checked, use that, otherwise, go with the default value from the hidden input.
Edit
Turned my comment into a fiddle, I've also added another field, a text field, to show better the idea behind it: http://jsfiddle.net/45qup/
Hope it helps!
Write up the click event for the checkbox..
$('#active').on('click', function(){
var isChecked = this.checked;
var val = 0;
if(isChecked){
val = 1
}
});
Try somthing like
$("form#formID :input").each(function(){
if ($(this).attr() == 'checkbox') return $(this).checked();
else return $(this).val();
});
Not sure if if I’d go with this ;) , but it works:
var getCheckBoxValue = function() {
var values = $('[name="active"]')
.filter(':checked, :hidden')
.map(function(){
return parseInt($(this).val(), 10);
}
);
return Math.max.apply(Math, values);
}
http://jsfiddle.net/Xc5H7/1/
Inspired by #Deleteman's idea, this is a slightly simpler way of doing it:
var getCheckBoxValue = function() {
var input = $('[name="active"]');
return $(input[1].checked ? input[1] : input[0]).val();
}
This assumes there's only two fields with this name, which is a sane assumption for what you're trying to do.
It also assumes the hidden field always comes before the checkbox, which again, since this is, I assume, for Rails, is a sane assumption :)
I have a form which contains couple fields. Its very easy to validate this form. But when I'm using append or clone comand and add couple more fields in it dynamically I cannot validate the appended fields.
Here is the my code:
function addone(container, new_div) {
var to_copy = document.getElementById(new_div);
$(to_copy).clone(true).insertAfter(to_copy);
}
And because it doesn't matter which fields and I want all of them get field out I used class instead of id.
$(document).ready(function(){
$('#add_size').live('click', function(){
if($('.inp').val() == "") {
alert('Need to fill-out all fields')
}
else {
alert('Thanks')
}
})
})
Any idea? Thanks in advance.
$(document).ready(function(){
$('#add_size').live('click', function(){
if( ! checkvalid() ) {
alert('Need to fill-out all fields')
}
else {
alert('Thanks')
}
})
})
function checkvalid(){
var valid = true;
$('.inp').each(function(){
if (this.value == '') {
valid = false;
return;
}
})
return valid;
}
I see one thing that might cause you trouble...:
If you're only going to check fields for validity on submit, then I don't think you need the live handler. You're not adding fields with #add_size, you're adding .inp's. Just do your validations on click, and jQuery should find all the .inp class fields that are there at the time of the event:
$('#add_size').click(function(
$('.inp').each ...
)};
Or maybe I totally read the question wrong...
Hey all. I've been been trying to figure this out for a while now.
I create a jQuery object of checkboxes and store it in a variable:
$group1 = $('#checkbox1,#checkbox2,#checkbox3,#checkbox4');
The user cannot continue unless all checkboxes in that group are checked.
I've been using an if statement combined with .is(':checked') to find a boolean value:
if( $group1.is(':checked') ){
//continue is OK
}
...but .is(':checked') will return TRUE if any checkboxes are checked within the group. Essentially, .is(':checked') performs an OR operation on the selected elements in $group1. I'm looking for an AND operation, so all selected elements must be checked to return TRUE. Is there a jQuery function that does this, or another workaround?
#Adam is off just a bit
if( $group1.filter(':not(:checked)').length === 0){
//continue is OK
}
Corrected:
You could filter to get only the elements that are not checked, and then check to see if any are any elements still in the collection, if there are not than all the elements in the group are checked:
if( $group1.filter(':not(:checked)').length === 0){
//continue is OK
}
I think you need something like:
$(document).ready(function(){
var checked = true;
$('input:checkbox').each(function(){
if(checked){
checked = $(this).is(':checked');
}
});
});
This should set checked = false if any of them are unchecked.
I would suggest that you give your checkboxes a a class then
var len = $('.check_needed').length;
var chk = $('.check_needed:checked').length;
if (len == check){
//carry on
}else{
// go home
}
You can use also
$(function(){
// add multiple select / deselect functionality
$("#selec_all_chk").click(function () {
$('.chk_class').attr('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// else uncheck
$(".chk_class").click(function(){
if($(".chk_class").length == $(".chk_class:checked").length) {
$("#selec_all_chk").attr("checked", "checked");
} else {
$("#selec_all_chk").removeAttr("checked");
}
});
});