I have this code , it is reading the value from a text box, I thought checking for '' before trying to parseInt() would be safe , but apparently not.
I am getting the error:
Uncaught TypeError: Cannot call method 'toLowerCase' of undefined
With this code:
var total = 0;
$.each('.reg-input', function () {
if ($(this).val() == '') {
}
else {
total += parseInt($(this).val()); //this line throwing error
}
});
if (total == 0) {
$('.RegisterContainer').hide();
}
I have this code , it is reading the value from a text box, I thought checking for '' before trying to parseInt() would be safe , but apparently not.
I am getting the error:
Uncaught TypeError: Cannot call method 'toLowerCase' of undefined
With this code:
var total = 0;
$.each('.reg-input', function () {
if ($(this).val() == '') {
}
else {
total += parseInt($(this).val()); //this line throwing error
}
});
if (total == 0) {
$('.RegisterContainer').hide();
}
Share
Improve this question
edited Aug 26, 2017 at 7:11
Cœur
38.7k26 gold badges203 silver badges277 bronze badges
asked Sep 26, 2012 at 19:32
Scott SelbyScott Selby
9,58012 gold badges62 silver badges97 bronze badges
5
-
1
Try fixing the brackets
total += parseInt($(this).val());
– Selvakumar Arumugam Commented Sep 26, 2012 at 19:34 -
Shouldn't it be
total += parseInt(this.val());
? – Shmiddty Commented Sep 26, 2012 at 19:34 - that was pasting it problem , I have the right bracket in code – Scott Selby Commented Sep 26, 2012 at 19:35
-
1
Has no one read the error? Where are you actually using
'toLowerCase'
?!? Wherever you're calling.toLowerCase()
the object being referenced isundefined
. – zzzzBov Commented Sep 26, 2012 at 19:38 -
@zzzzBov It's within the
$()
method or.val()
depending on where it fails. he's iterating through a string, not a collection of elements. That's where the real problem lies. – Kevin B Commented Sep 26, 2012 at 19:40
4 Answers
Reset to default 7'.reg-input'
is a string, this
in that case will also be a string, not a dom element. Try this instead:
$('.reg-input').each(function(){...
The reason that you get the strange error is that you are calling $.each
with a string instead of a collection.
A string is an object, so it's still a collection, but the items in the collection is the properties and methods of the string class (and toLowerCase
is one of them).
To loop through the elements that you find with a selector, you use the each
method instead of the $.each
method:
$('.reg-input').each(function(){
..
});
Now this
inside the loop will be an element, not a member of the string class, so you can use $(this).val()
to get the values.
I had the exact same problem, and I solved it by assigning $(this).val()
to a variable and then parse the variable to an integer. So a correct workaround would be:
var total = 0;
$.each('.reg-input', function () {
if ($(this).val() == '') {
}
else {
$this_val = $(this).val();
total += parseInt($this_val); //this line throwing error
}
});
if (total == 0) {
$('.RegisterContainer').hide();
}
if ($(this).val() == "")
checks to see if the value is the string "" not undefined.
You can use the typeof operator like this
if(typeof $(this).val() == 'undefined')