LmC LmC - 9 months ago 46
HTML Question

Jquery Each Input Within DIV (Has A Table)

Im currently trying to get all input elements from a DIV.

$("[required]").each(function() {


I have this code which returns every element with a

I know within the function of that
I can use each item like:


So I get the div ID's shown below, and try to get all the inputs from it via:

var id = $(this).attr('id');
console.log("### " + id);
console.log($("#" + id + " > :input").length);

Which returns 0 when I have 4 in put elements within that DIV (also theres a table in the div).

What I would ideally like to do is for each input element within my div, print its ID.


<div id="contactAddress" required="true">

<td>Addres line 1:</td>
<td colspan="2">
<input type="text"/>
<td>Addres line 2:</td>
<td colspan="2">
<input type="text" />


Shows nothign but if i add anythign outsire the or like below...

<div id="contactAddress" required="true">
<input type="text" />

And run
It shows it put not the table?

Any ideas im using Jquery Mobile

Answer Source

This below will find all inputs inside the div element. It will then leave a log of the id of this element.

$("div > input").each(function() {
  console.log( $(this).attr("id") );

if you need the div id containing the inputs you could use .parent()

$("input").each(function() {
  console.log( $(this).parent().attr("id") );