mohammad eslahi sani
mohammad eslahi sani

Reputation: 674

get the value of input type text in js

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

Answers (1)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

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

DEMO

Upvotes: 1

Related Questions