0

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); }); 
3
  • 1
    Use getElementByName, not getElementById. But can easily be done by JQuery Commented Jul 30, 2015 at 13:13
  • getElementsByName, not getElementByName. This will return an array of elements Commented Jul 30, 2015 at 13:23
  • 1
    what version of jQuery are you using? live() has been deprecated for years Commented Jul 30, 2015 at 13:23

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

assumes that OP is using a version that supports on(). Could be working on an old project that hasn't been upgraded
I upgraded Jquery and then this code solved my problem, thanks
@mohammadeslahisani Glad to help you. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.