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>
Liam
  • 27,717
  • 28
  • 128
  • 190
akauts
  • 153
  • 12

5 Answers5

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!

Community
  • 1
  • 1
Liam
  • 27,717
  • 28
  • 128
  • 190
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">
Rino Raj
  • 6,264
  • 2
  • 27
  • 42
  • Always glad to help. – Rino Raj Mar 29 '16 at 10:37
  • 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. – akauts Mar 29 '16 at 10:39
  • 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); }); – akauts Mar 29 '16 at 10:42
  • 1
    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')` – Rino Raj Mar 29 '16 at 10:42
  • 2
    looping using `.each()` is also fine – Rino Raj Mar 29 '16 at 10:43
  • this **still** has invalid markup. Just because you can work around the invalid markup doesn't mean you should. – Liam Mar 29 '16 at 12:13
0

Try this:

console.log($("[id^=txtItemQuantity]").eq(1).val());
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
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
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
-1

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

dpaul1994
  • 332
  • 3
  • 16
  • 1
    I doubt this will work because if you had 3 elements with the same id the markup would be invalid, thus likely breaking jquery – Liam Mar 29 '16 at 10:19
  • 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 – dpaul1994 Mar 29 '16 at 10:22
  • Well sorry then :) – dpaul1994 Mar 29 '16 at 10:26
  • 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 ;) – dpaul1994 Mar 29 '16 at 10:28