0

I am trying to find the value of an input statement that does not have an id.

The html is:

<td id="LastName">
    <input type="text" value="Abner">
</td>

The jQuery code I am trying to use is:

var name = $('#LastName').find('input').value

But it keeps coming up empty. I thought I had read about .find() properly, but apparently not.

Anyone see my error?

Keith Clark
  • 609
  • 1
  • 7
  • 19
  • use `var name = $('#LastName').find('input').val()` , `value` is for getting the value from a DOM node using regular javascript `.val()` gets the value from a jQuery object, see also http://stackoverflow.com/questions/7322078/jqueryid-val-vs-getelementbyidid-value\ – Wesley Smith Oct 06 '16 at 23:36
  • 1
    Dag nabit! I knew that. One of those forest for the trees goofs. – Keith Clark Oct 06 '16 at 23:43

1 Answers1

0

As DelightedD0D commented, .val() is the correct way to retrieve a value from a text input.

Here's a working snippet: https://jsfiddle.net/tuywqucb/3/

One other thing to look out for.. if the td isn't correctly wrapped in a tr and a table tag, many browsers will omit it from the DOM which will make your element and thus your id disappear and thus not be found by jQuery.

Julian
  • 424
  • 2
  • 8
  • Questions like this dont really need an answer. It should really be deleted by the asker or voted to close as *"This question was caused by a problem that can no longer be reproduced or a simple typographical error."* Additionally, I would posit that the OP left out the `tr` and `table` tags on purpose here because they were not relevant to the question ;) – Wesley Smith Oct 07 '16 at 00:30