javascript - $('#id').value is undefined - Stack Overflow

I am having trouble with trying to check the length of a field and set a value based on the result. Usi

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.

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.

Share Improve this question edited Aug 16, 2014 at 18:38 user2864740 62.1k15 gold badges158 silver badges228 bronze badges asked Aug 16, 2014 at 18:18 Luke StowardLuke Stoward 1,54015 silver badges25 bronze badges 1
  • The error message doesn't lie. Note how much more relevant the updated title is. – user2864740 Commented Aug 16, 2014 at 18:38
Add a ment  | 

3 Answers 3

Reset to default 7

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;

try this

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

Instead of

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

Change those variables to this:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744406874a4572677.html

相关推荐

  • javascript - $(&#39;#id&#39;).value is undefined - Stack Overflow

    I am having trouble with trying to check the length of a field and set a value based on the result. Usi

    6天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信