$('#id').value is undefined

I am having trouble with trying to check the length of a field and set a value based on the result. Using firebug with firefox I keep getting the error 'elUsername' is not defined. Please tell me what I am doing wrong. Fairly new to JQuery.

$('#usernameInput').blur(function ()
{
    var elUsername = $('#usernameInput').value;
    var elUsernameFeedback = $('#usernameFeedback');
    if (elUsername.length < 5) {
        elUsernameFeedback.text("Please enter your username");
    }
});

Basically when the user leaves the field and the username is less than 5 (just for testing) the gets the text set to it to prompt the user to correct the mistake. However the if statement is never entered. Any help would be greatly appreciated.

Answers:

Answer

You're trying to use a native JavaScript property on a jQuery object. Use :

var elUsername = $('#usernameInput').val();

or

var elUsername = document.getElementById('usernameInput').value;

or the slightly less obvious, but still valid:

var elUsername = $('#usernameInput')[0].value;
var elUsername = $('#usernameInput').get(0).value;
Answer

try this

var elUsername = $('#usernameInput').val();

Instead of

var elUsername = $('#usernameInput').value;
Answer

Change those variables to this:

var elUsername = $('#usernameInput').val();

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.