user1099407
user1099407

Reputation:

Error javascript can not get value from attribute value

$(document).ready(function(){
    $('.submit').click(function(){
         var value = $('#Name').attr('value');
        alert(value); 
    });  
});
<input type="text" value="10" id="Name"/>Hello world
<input type="submit" class="submit" value="submit"/>

Result is "Hello World", I want get value is "10", how to fix it ?

Upvotes: 1

Views: 66

Answers (2)

Sameera Thilakasiri
Sameera Thilakasiri

Reputation: 9508

Working fiddle of example,

Upvotes: 0

Adam Rackis
Adam Rackis

Reputation: 83358

I'm not sure why you're getting Hello World, but to retrieve an input's value, use the val() function

var value = $('#Name').val();

You don't have a second input on your page anywhere with an id of "Name", do you?


Also, depending on what you want to do, you may be better off handler the form's submit event, rather than the submit button's click handler:

$(document).ready(function(){
    $('form').submit(function(){
         var value = $('#Name').val();
        alert(value); 
    });  
});

Upvotes: 3

Related Questions