1
<form action="register.php" method="post" id="regform">
    <h4>Login info</h4>
    <hr><br>
    <input type="text" name="username" placeholder="Username"><br><br>
    <input type="password" name="password" placeholder="Password"><br><br>
</form>

--

$(document).ready(function(){
    $('#regform').on('submit', function(){
        alert($(this).username.val());
        return false;
    });
});

--

The error I'm getting:

TypeError: $(...).username is undefined

Can't see what I'm doing wrong..

3 Answers 3

2

You could use the following:

$(this).find('[name="username"]').val()

Example

or alternatively this.username.value would work too.

Sign up to request clarification or add additional context in comments.

Comments

2

It should be:

alert($(this).find('[name=username]').val());

or:

alert(this.username.value);

Accessing input elements as properties of the containing form is a DOM feature, not part of jQuery.

Comments

1

Looks like you meant to do:

$(this).find("input[name=username]").val()

You can't just append .username to the end of a jquery object and hope for the right returned value. You need to select the correct element from the dom.

Also, instead of "return false" you should do this:

$('#regform').on('submit', function(e){
    e.preventDefault();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.