andrean
andrean

Reputation: 107

maxLength is not a function

I have a problem with an html page

here a simplified version of the page

<html>
    <head>
        <script type="text/javascript">

            function maxLength()
            {
                console.log('maxLength');
            }

        </script>
    </head>
    <body>
        <input type"text" onblur="maxLength()">
    </body>
</html>

on onblur event I receive this error

Uncaught TypeError: maxLength is not a function

but if i call the function from console it works without errors and print 'maxLength'

Why?

Upvotes: 2

Views: 1213

Answers (2)

adityap
adityap

Reputation: 351

If you rename the function to something else it should work fine for you. The explanation is aptly given with Alex already. Example:

<html>
    <head>
        <script type="text/javascript">

            function maximumLength()
            {
                console.log('maxLength');
            }

        </script>
    </head>
    <body>
        <input type"text" onblur="maximumLength()">
    </body>
</html>

Upvotes: 0

alex
alex

Reputation: 490213

The code there runs in the context of the input, which has defined maxLength.

Better developer tools would make it clearer:

maxLength is not a function at HTMLInputElement.onblur

Rename your function to something else or use a different way of attaching the event listener.

Upvotes: 4

Related Questions