Reputation: 674
In front page I have a input with type text. A button is created aside it. When button is clicked, in js I want to use the text in the input. But I get this error:
Cannot read property 'value' of null
frontpage:
New Name: <input type="text" name="newName" value= "blahblah" > </input>
<button id="submitNewName">OK!</button>
Js:
$("#submitNewName").live('click',function(){
alert(document.getElementById('newName').value);
});
Upvotes: 0
Views: 88
Reputation: 67207
Try to use attribute selector
to grab the element,
$("#submitNewName").on('click',function(){
alert($('[name=newName]').val());
});
As well as, don't use live()
since it was deprecated in recent versions of jquery
Upvotes: 1