user3053216
user3053216

Reputation: 809

jquery find function not working

I have html with 2

<select> ... </select>

elements inside a span with class spanClass. Now i am trying to select those selects with jQuery with this code:

jQuery(document).ready(function() {
    spans = jQuery('.spanClass');
    spans.each(function() {

        var inputs = jQuery(this).find('select');
        console.log(inputs);// This is working
        inputs.each(function() {
            alert('test'); //This not
       });
    });
});

HTML:

<table>
<tr>
    <td>
        <select name="een">
            <option> test </option>
        </select>
    </td>
</tr>

        <select name="twee">
            <option> test </option>
        </select>
    </td>
</tr>
</table>

However, this is not working, can anybody tell me why?

Upvotes: 0

Views: 2845

Answers (2)

Ramin Bateni
Ramin Bateni

Reputation: 17405

First> Put Table inside Div instead of Span (it's the correct way to do this)

Second> Correct your table tags as the following image and codes (some of them are incorrect!)

Now> Use these codes

HTML:

<div class="divClass">
<table>
    <tr>
        <td>
            <select name="een">
                <option> test </option>
            </select>
        </td>
    </tr>
    <tr>
        <td>
            <select name="twee">
                <option> test </option>
            </select>
        </td>
    </tr>
</table>
</div>

jQuery:

jQuery(document).ready(function() {
    spans = jQuery('.divClass');
    spans.each(function() {

        var inputs = jQuery(this).find('select');
        console.log(inputs);
        inputs.each(function() {
            console.log(jQuery(this).prop("name")); 
       });
    });
});

Result:

enter image description here

Upvotes: 1

sujin
sujin

Reputation: 34

var inputs = jQuery(this).find('select');   // Isn't inputs empty jQueryObject? length 0?

if inputs is empty jQueryObject, the callback function passed by .each() is never called.

Upvotes: 0

Related Questions