user3760639
user3760639

Reputation: 73

Beginner Javascript - Object is not a function

Hey guys taking a stab at my first Javascript validation but getting stuck pretty early on. I am calling a JS function from onkeyup="" but I receive the error:

Object is not a function

Code:

<html>
    <head>
         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" ></script>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
        <h3> My Sign Up Form</h3>
        <form name="signup">
            <label>First Name<input type="text" name="fname" onkeyup="fname()"/></label><div id="fnameVal"></div><br>
            <label>Last Name<input type="text" name="lastname" onkeyup="fourchars();"/></label><div id="lnameVal"></div><br>
            <!--<label>Email<input type="text" name="email"/></label><div id="email"></div><br>
            <label>Password<input type="password" name="password"/><div id="password"></div></label><br>
            <label>ConfirmPassword<input type="password" name="confirmpassword"/><div id="confirmpassword"></div></label><br>-->
            <label><input type="submit" value="Submit"/></label> 
         </form>
    </body>
    <script>    
        function fname(){
            alert('jjjj');
        }
    </script>
</html>

Upvotes: 0

Views: 62

Answers (5)

Eshant Sahu
Eshant Sahu

Reputation: 360

you can't have your function name same as your control name. i.e. fname & fname()

            <label>First Name<input type="text" name="fname" onkeyup="validateFname()"/></label><div id="fname"></div>

OK, Try something like this.

Upvotes: 1

Farshad
Farshad

Reputation: 1485

You can use jquery on keyup

$(document).ready(function() {

$('body').on("keyup", "input[name=fname]", function() {
    alert($(this).val());
    // do your stuff
});


});

JSFIDDLE DEMO

Upvotes: 0

Afsa
Afsa

Reputation: 2125

It seems as fname is a reference to your element in your form. Try changing the name of the function or the name of element.

function fnameFunc() {
   alert('jjjj');
}

And the HTML

<input type="text" name="fname" onkeyup="fnameFunc()"/>

Upvotes: 0

scipilot
scipilot

Reputation: 7447

Raname your function to be different from the input name.

e.g.

<script>
    function valname(){
      alert('jjjj');
 }
</script>
</head>
<body>
    <h3> My Sign Up Form</h3>
    <form name="signup">

    <label>First Name<input type="text" name="fname" onkeyup="valname()"/></label><div id="fnameVal"></div><br>

Upvotes: 0

Vinc199789
Vinc199789

Reputation: 1046

replace your onkeyup to this. I know it is the same code but sometimes it works when you relace you code by the same code form an other site. BTW it works fine if I test is on a site.

onkeyup="fname()"

Upvotes: 0

Related Questions