function checkInputData() {
$('.mainSearchSubmit').live('click', function () {
var inputData = $(this).parent().children().eq(0).val();
console.log(inputData.length);
//this returns undefined
console.log(inputData);
//and this returns text from inpu so i know there is data
});
}
Any ideas why is this happening, in other cases when retrieving val() from input it always comes as string??
The length property returns the length of a string. The length property of an empty string is 0.
The length function in Javascript is used to return the length of an object. And since length is a property of an object it can be used on both arrays and strings.
A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .
Java String length method() The Java String class contains a length() method that returns the total number of characters a given String contains. This value includes all blanks, spaces, and other special characters. Every character in the String is counted.
The only explanation to length
being undefined is if inputData is not a string. You neglected to mention what type of input you're working with, but in any case, casting to string should solve the issue:
function checkInputData() {
$('.mainSearchSubmit').live('click', function () {
var inputData = $(this).parent().children().eq(0).val();
inputData = String(inputData); // Cast to string
console.log(inputData.length);
//this returns undefined
console.log(inputData);
//and this returns text from inpu so i know there is data
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With