Top Ad unit 728 × 90

Uncaught TypeError: length is not a function - Java Script / JQuery

I have started a project with PHP and Microsoft SQL. Here I require a form where one text box will present in the form. Whenever someone enter a number or a digit into the text box then it will converted two digits in lost focus or blur function of the Java Script/JQuery. If someone enter two digits then nothing will happen but if user enter only one digit then only it will become two digits adding zero before the number. So for that I have written the following code

In Java Script/JQuery code:

$(document).ready( function() {
        $("#txtn_minor_code").on("blur", function(){
            var n_minor_code = $("#txtn_minor_code").val(); //alert(n_minor_code);  //output is 9
            var len_mc = length(n_minor_code); alert(len_mc);
            if(len_mc === 1){
                n_minor_code = '0' + n_minor_code;
                $("#txtn_minor_code").val(n_minor_code);
            }
        });
    });

But this code showing an error message ' Uncaught TypeError: length is not a function'



After that I have the following code but the error was same in the console window.

$(document).ready( function() {
        $("#txtn_minor_code").on("blur", function(){
            var n_minor_code = $("#txtn_minor_code").val(); //alert(n_minor_code);//output is 9
            var len_mc = n_minor_code.length(); alert(len_mc);
            if(len_mc === 1){
                n_minor_code = '0' + n_minor_code;
                $("#txtn_minor_code").val(n_minor_code);
            }
        });
    });

Finally when I remove the parenthesis from length word then it gives the correct result like below and I understood that in java-script / jquery, length is property, it is not a function. So I removed this ().

$(document).ready( function() {
        $("#txtn_minor_code").on("blur", function(){
            var n_minor_code = $("#txtn_minor_code").val(); //alert(n_minor_code);//output is 9
            var len_mc = n_minor_code.length; alert(len_mc);  //It is correct
            if(len_mc === 1){
                n_minor_code = '0' + n_minor_code;
                $("#txtn_minor_code").val(n_minor_code); // Now output is 09
            }
        });
    });

So in summary, don't use the code below
           length(n_minor_code); // It is not correct
           n_minor_code.length(); // It is not correct

Use the code below
           n_minor_code.length; // It is correct



If you need any help, please feel free to ask your question in the  comment box below. I will definitely try to answer you as early as possible.
Uncaught TypeError: length is not a function - Java Script / JQuery Reviewed by Ashok Sen on 16:17:00 Rating: 5

No comments:

Website Design Company at Kolkata All Rights Reserved © 2014 - 2019
Developed by Asenwebmedia

Contact Form

Name

Email *

Message *

Powered by Blogger.