I have a variable named countTicked
that is storing an integer of how many relatedBoxes
there are on the page.
I need an if
statement that does something when the value of countTicked
is greater than 1.
if (!$(clickedCheckBox).is(':checked'))
{
//do something
}
else
{
var selectedDots = $(currentMap).find('div.' + checkBoxID);
var relatedCheckBoxes = $(selectedDots).attr('class');
var countTicked = $(relatedCheckBoxes).filter(':checked');
if (countTicked > 1)
{
//do one thing
}
else
{
//do something else
}
}
I have implemented the changes you guys have provided. But my second IF statement still doesnt work. What am I still doing wrong?
I have a variable named countTicked
that is storing an integer of how many relatedBoxes
there are on the page.
I need an if
statement that does something when the value of countTicked
is greater than 1.
if (!$(clickedCheckBox).is(':checked'))
{
//do something
}
else
{
var selectedDots = $(currentMap).find('div.' + checkBoxID);
var relatedCheckBoxes = $(selectedDots).attr('class');
var countTicked = $(relatedCheckBoxes).filter(':checked');
if (countTicked > 1)
{
//do one thing
}
else
{
//do something else
}
}
I have implemented the changes you guys have provided. But my second IF statement still doesnt work. What am I still doing wrong?
Share Improve this question edited Dec 28, 2011 at 9:52 Cecil Theodore asked Dec 28, 2011 at 8:58 Cecil TheodoreCecil Theodore 9,95910 gold badges32 silver badges38 bronze badges 12- What is relatedBoxes? names of the elements? class? a variable? – gdoron Commented Dec 28, 2011 at 9:02
-
1
Since you're using the
length
property of a jQuery object, you don't have to useparseInt
(it's already a number). – Rob W Commented Dec 28, 2011 at 9:03 - When you use parseInt, make sure to pass the second variable as well, like this in your case: parseInt($(relatedBoxes).length, 10); otherwise weird things can happen. – Christofer Eliasson Commented Dec 28, 2011 at 9:03
-
3
@ChristoferEliasson That's generally a good advice, but it doesn't matter in this case, as the
length
property of a jQuery object is a number, in base-10. – Rob W Commented Dec 28, 2011 at 9:04 -
2
$
is just a function -- an alias forjQuery
in this case. So, ask yourself: what does/shouldjQuery(countTicked)
do? – user166390 Commented Dec 28, 2011 at 9:07
4 Answers
Reset to default 3Just use this as if condition:
if (countTicked > 1)
countTicked contains an integer, no need to put it in a jquery object.
Why do use use the $ sign? what is relatedBoxes?
var countTicked = parseInt($(relatedBoxes).length);
if (countTicked > 1)
{
//do something
}
else
{
//do something else
}
Update: you have to use the length to get the number of elements.
var countTicked = $(relatedCheckBoxes).filter(':checked').length;
it should be :
var countTicked = parseInt($(relatedBoxes).length);
if (countTicked > 1)
{
//do something
} else
{
//do something else
}
as you are using countTicked
as variable. No need to use it as a jquery object
$(relatedCheckBoxes).filter(':checked')
is a jQuery object, which has length
property.
So in order to know amount of ticked checkboxes, use
var countTicked = $(relatedCheckBoxes).filter(':checked').length
parseInt
isn't obligated.