Input box value coming as undefined
I am getting input box value as undefined when appending to a div. Below is the code.
JS:
$('#submitHook').click(function () {
var v = ('#try').val();
alert(v);
$('#container').prepend('<tr><td>' + v + '</td></tr>');
});
HTML:
<code>
<input type="text" id="try"></in开发者_StackOverflow中文版put>
<input type="button" id="submitHook"></input>
<table id="container></table>
</code>
Also how can the same be exteneded to a textarea? Appreciate any help.
You're missing a $ before ('#try')
$('#submitHook').click(function(){
var v = $('#try').val();
alert(v);
$('#container').prepend(''+v+'');
});
You're missing a $
sign (the jQuery object). This should work:
$('#submitHook').click(function(){ var v = $('#try').val(); alert(v); $('#container').prepend(''+v+''); });
You should be able to do exactly the same thing with a textarea too, just reference its ID attribute, like you have done here with #try
.
You forgot to use the jQuery selector ($) when you assign the value to v.
Here's the updated code:
$('#submitHook').click(function () {
var v = ** $. ** ('#try').val();
alert(v);
$('#container').prepend('' + v + '');
});
精彩评论