Luke Stoward
Luke Stoward

Reputation: 1530

$('#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.

Upvotes: 1

Views: 2468

Answers (3)

Richie
Richie

Reputation: 1439

Change those variables to this:

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

Upvotes: 0

user3713959
user3713959

Reputation: 101

try this

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

Instead of

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

Upvotes: 3

j08691
j08691

Reputation: 207861

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;

Upvotes: 7

Related Questions