6

I have something like the following

<div id="container">
      <ul id="name_list">
          <li class="name">
             <input type="hidden" class="name_val" value="5" />
          </li>
      </ul>
</div>

I am trying to get the value of the input. So far I have the following Jquery statment.

$("#container li.name input.name_val").val();

this does not work anyone know a better way?

1
  • don't be unnecessarily specific, $('container').find('input').val() or even just $('.name_val').val() should do the job. Commented Apr 13, 2010 at 13:02

3 Answers 3

12

You were having a typo in your input class name

$("#container li.name input.name_val").val();

Also you can change your selector to something like

$("#name_list > li.name > input:hidden.name_val").val();
Sign up to request clarification or add additional context in comments.

2 Comments

sorry that was a typo on my part, this does not work for me should it?
Make sure that you don't have multiple elements with the same id.
3

Give your <input> either a name or an id. The you can get the value in either of these two ways:

$('input[name=thename]').val()

or

$('input#theid').val()

Comments

1

Should it be

var value = $('input.name_val').val();

You don't need to mention the other parts of the dom, just the area you are interested in.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.