-5

the problem is more complicated with loops, but all I need is how to get value of:

input id="product_addons_0_selections_2_id"

by accessing div with Id product_addons_0_selections_template2 as ex:

<div id="product_addons_0_selections_template2">    
   <input type="text" id="i_dont_need_this">
   <input type="text" id="product_addons_0_selections_2_id">
</div>

Thank you

Fi3n1k
  • 863
  • 3
  • 12
  • 20
  • 6
    IDs must be unique, so just select it by the ID. – j08691 Nov 30 '15 at 15:46
  • Possible duplicate of [Get the value in an input text box](http://stackoverflow.com/questions/4088467/get-the-value-in-an-input-text-box) – matt. Nov 30 '15 at 16:09
  • if Id is not unique like in my case because if loop ? – Fi3n1k Nov 30 '15 at 16:10
  • In that case you need to change your loop. Your markup is invalid and that's not something that should be worked around. It's something that should be fixed. – War10ck Nov 30 '15 at 21:31

3 Answers3

3

You can get the contents of the input element with id product_addons_0_selections_template2 by this statement:

var value = $('#product_addons_0_selections_2_id').val();
Baklap4
  • 3,914
  • 2
  • 29
  • 56
2

This example shows you how to get the Value of a input:

var bla = $('#myInputID').val();

If you have a couple inside a container, you should iterate them:

$.each($('#product_addons_0_selections_template2').children(), function(){ 
    //Here you can ask for the id of the object: $(this).attr('id');
    var value = $(this).val();
}
pvergne
  • 21
  • 3
0

You can use this

$('#product_addons_0_selections_2_id').val()

Here is an example, type something in the second input and click, it should give you the value https://jsfiddle.net/qqjkzuxk/

Ant
  • 98
  • 1
  • 2
  • 11