0

I have 3 input with same id how to get the 2nd input value for example? I use eq() and nth-child(), doesn't work

console.log($("#txtItemQuantity:eq(2)").val());

Note: I want to use id as the identifier, not class

HTML

<div class="masterItem">
    <div class="row oItem">
        <div class="col-md-2">
            <div class="form-group">
                <div class="input-group"><span class="input-group-addon" style="font-weight:bold;color:#330099">Name</span><input id="txtItemName" class="form-control" placeholder="Name" aria-describedby="basic-addon1" type="text"></div>
                <div class="input-group"><span class="input-group-addon" style="font-weight:bold;color:#330099">Quantity</span><input id="txtItemQuantity" class="form-control" placeholder="Quantity" aria-describedby="basic-addon1" type="text"></div>
            </div>
        </div>
    </div>
    <div class="row oItem">
        <div class="col-md-2">
            <div class="form-group">
                <div class="input-group"><span class="input-group-addon" style="font-weight:bold;color:#330099">Name</span><input id="txtItemName" class="form-control" placeholder="Name" aria-describedby="basic-addon1" type="text"></div>
                <div class="input-group"><span class="input-group-addon" style="font-weight:bold;color:#330099">Quantity</span><input id="txtItemQuantity" class="form-control" placeholder="Quantity" aria-describedby="basic-addon1" type="text"></div>
            </div>
        </div>
    </div>
    <div class="row oItem">
        <div class="col-md-2">
            <div class="form-group">
                <div class="input-group"><span class="input-group-addon" style="font-weight:bold;color:#330099">Name</span><input id="txtItemName" class="form-control" placeholder="Name" aria-describedby="basic-addon1" type="text"></div>
                <div class="input-group"><span class="input-group-addon" style="font-weight:bold;color:#330099">Quantity</span><input id="txtItemQuantity" class="form-control" placeholder="Quantity" aria-describedby="basic-addon1" type="text"></div>
            </div>
        </div>
    </div>
</div>
13
  • same id ID should be unique use class instead Commented Mar 29, 2016 at 10:14
  • 2
    i have 3 input with same id Id should always be unique. Use data-id instead. Commented Mar 29, 2016 at 10:14
  • console.log($("input:eq(2)").val()); But ID must be unique Commented Mar 29, 2016 at 10:15
  • 1
    Tell this customer required, and avoid downvotes! Commented Mar 29, 2016 at 10:16
  • you can do both way but can't use same id tow or more time. Commented Mar 29, 2016 at 10:17

5 Answers 5

2

Nobody is providing the correct solution here. Your underlying issue is your invalid markup. No two elements in your HTML should have the same id. To do so breaks the W3C conventions and will produce unpredictable results. Not just here but in other things too.

Change your markup so that the id is unique and add a class, then select by this:

<div class="masterItem">
    <div class="row oItem">
        <div class="col-md-2">
            <div class="form-group">
                <div class="input-group"><span class="input-group-addon" style="font-weight:bold;color:#330099">Name</span>
                    <input id="txtItemName1" class="txtItemName form-control" placeholder="Name" aria-describedby="basic-addon1" type="text"></div>
                <div class="input-group"><span class="input-group-addon" style="font-weight:bold;color:#330099">Quantity</span>
                   <input id="txtItemQuantity1" class="txtItemQuantity form-control" placeholder="Quantity" aria-describedby="basic-addon1" type="text"></div>
            </div>
        </div>
    </div>
    <div class="row oItem">
        <div class="col-md-2">
            <div class="form-group">
                <div class="input-group"><span class="input-group-addon" style="font-weight:bold;color:#330099">Name</span>
                         <input id="txtItemName2" class="txtItemName form-control" placeholder="Name" aria-describedby="basic-addon1" type="text"></div>
                <div class="input-group"><span class="input-group-addon" style="font-weight:bold;color:#330099">Quantity</span>
                         <input id="txtItemQuantity2" class="txtItemQuantity form-control" placeholder="Quantity" aria-describedby="basic-addon1" type="text"></div>
            </div>
        </div>
    </div>
    <div class="row oItem">
        <div class="col-md-2">
            <div class="form-group">
                <div class="input-group"><span class="input-group-addon" style="font-weight:bold;color:#330099">Name</span>
                          <input id="txtItemName3" class="txtItemName form-control" placeholder="Name" aria-describedby="basic-addon1" type="text"></div>
                <div class="input-group"><span class="input-group-addon" style="font-weight:bold;color:#330099">Quantity</span>
                        <input id="txtItemQuantity3" class="txtItemQuantity form-control" placeholder="Quantity" aria-describedby="basic-addon1" type="text"></div>
            </div>
        </div>
    </div>
</div>

shortened:

<input id="txtItemName1" class="txtItemName form-control" type="text">

now just tweak your jquery selector (from your original question) to use the class selector (.) not the id selector (#).

console.log($(".txtItemQuantity:eq(2)").val());

FYI, because you have not provided names and because your input's share the same id the results posted back (when the form is posted) will also be unpredictable. Fix your underlying issue!

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

Comments

1

ID must be unique. It cannot repeat.

You can select using attribute id, like

$('[id="txtItemQuantity"]').eq('1')

Working Demo

alert($('[id="txtItemQuantity"]').eq('1').val())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input tpye="text" id="txtItemQuantity" value="1">
<input tpye="text" id="txtItemQuantity" value="2">
<input tpye="text" id="txtItemQuantity" value="3">

6 Comments

Always glad to help.
is it wrong if i insist to use id than class in this case, apparently it still has a solution? thank you again for your excellent solution Sir.
And my function to load array work like a charm! thank you sir. $.each(e.vntret,function(a,b){$('[id="txtItemQuantity"]').eq(a).val(b.item_qtyorder); });
Yes. it is always good to use a class where ever you are compelled to use same Id. If you are changing it to Id you can modify my answer like $('[class="txtItemQuantity"]').eq('1')
looping using .each() is also fine
|
0

jsfiddle link goes here

updated fiddle link with your HTML link

HTML

<div id="txtItemQuantity">
  <input value="1">
  <input value="2">
  <input value="3">
</div>

jQuery:

var res = $("#txtItemQuantity input:nth-child(2)").val();
alert(res); // result 2

2 Comments

thanks, i saw this in jquery learning page. how about in my case? would you please see my html (updated), thanks
wait, let me check and do it for you.
0

Try this:

console.log($("[id^=txtItemQuantity]").eq(1).val());

2 Comments

eq always start with 0 index, so you have to use eq(1) for getting the second input of quantity.
Now its fine, and looks great.
-1

Try like this: console.log($("#txtItemQuantity").eq(1).val()); Dont' forget, count starts from 0

4 Comments

I doubt this will work because if you had 3 elements with the same id the markup would be invalid, thus likely breaking jquery
Well of course it will not work if you have 3 elements with the same id. The idea about id is to be unique. Otherwise you can't use eq, or you can maybe but is not ideal
Well sorry then :)
Question is: I have 3 input with same id how to get the 2nd input value for example?. My answer is good for what he asked. All answers are good ;)

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.