Kris.Mitchell Kris.Mitchell - 2 months ago 6
jQuery Question

Get child element of parent div jquery

I have a div

<div id="customFormContact">
.. other elements that don't matter ...
<input type="hidden" class="formID" value="Custom Product Contact" />
</div>


I am using the div to do a jquery dialog with the submit button doing some ajax work. In the ajax call I would like to do is pull out the value of the hidden field. What is the best way to do so?

I have tried

var id = $("#customFormContact, .formID").val();


As well as

var id = $("#customFormContact > .formID").val();


to no avail.

With an alert of the value, I get either a undefinded (with the first) or a blank with the second.

Answer

Leave off the , (multiple selector) and > (child selector), just use a space like this:

var id = $("#customFormContact .formID").val();

With only a space is a descendant selector, and it'll find a .formID as a child of any depth inside #customFormContact.

Comments