0

I have a question like so.

I have HTML structure:

<form id="form">
    <div class="row">
        <div class="item-input">
            <label for="A1">A1</label>
            <input type="text" name ="A1" />
            <span class="input-require">(*)</span>
            <span class="info">(Enter A1)</span>
        </div>
        <div class="item-input">
            <label for="A2">A2</label>
            <input type="text" name ="A2" />
            <span class="input-require">(*)</span>
            <span class="info">(Enter A2)</span>
        </div>
    </div>
    <div class="row">
        <div class="item-input">
            <label for="B1">B1</label>
            <input type="text" name ="B1" />
            <span class="input-require">(*)</span>
            <span class="info">(Enter B1)</span>
        </div>
        <div class="item-input">
            <label for="B2">B2</label>
            <input type="text" name ="B2" />
            <span class="input-require">(*)</span>
            <span class="info">(Enter B2)</span>
        </div>
    </div>
    ...
</form>

Now I want to selected 'span' has class "info" behind input[type=text] each. I try:

<script type="text/javascript"> 
    $(document).ready(function() {
        $("#form input[type=text]").each(function(){
            var spanInfo = $(this).closest("item-input").next().find('span.info');

            console.log(spanInfo.text()); // Result is empty
        });
    });
</script>

Why is empty? Help me please. How do I, thanks

2 Answers 2

1

var spanInfo = $(this).closest("item-input").next().find('span.info');

Should be

var spanInfo = $(this).closest(".item-input").next().find('span.info');

You are missing the . (dot, class)

Sign up to request clarification or add additional context in comments.

1 Comment

I was'nt careful. :) Thank you
0

Your mean is show "(Enter A2)" and "(Enter B2)" or show all 4 element value: (Enter A1), (Enter A2), (Enter B1), (Enter B2)

If only show 2 value then CODE HERE

else You want to show 4 value, you can code:

$(document).ready(function() {
    $("#form input[type=text]").each(function(){
        var spanInfo = $(this).closest(".item-input").find('span.info');

        console.log(spanInfo.text()); 
    });
});

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.