string.length returns undefined?

21,170

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
    });
 }
Share:
21,170
Davor Zubak
Author by

Davor Zubak

Updated on March 07, 2020

Comments

  • Davor Zubak
    Davor Zubak about 4 years
    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??

  • dante
    dante almost 12 years
    "" + $(this).parent().yadda().yadda() will also cast to a string