1
<input id="value_1" type="text" name="values_1" value="somthing">
<input id="value_2" type="text" name="values_2" value="somthing">
<input id="value_8" type="text" name="values_8" value="somthing">

Hello everyone. I searched the forum and can not find solution for this. What I want is to catch input id by low to high number and fill it with text.

I have add button for fields

$("#value_1").val("something"); 

this method working for exactly id number

$( "span input" ).first().val("something");

this working great but i dont know how to catch second and third.

Please help and thanks in advance.

2 Answers2

1

You can use :eq()

$('input:eq(0)').val(0);
$('input:eq(1)').val(1);
$('input:eq(2)').val(2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<input id="value_1" type="text" name="values_1" value="somthing">
<input id="value_2" type="text" name="values_2" value="somthing">
<input id="value_8" type="text" name="values_8" value="somthing">
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
1

I don't recommend this ID naming convention, but if you must,

// Iterate through input elements, checking if the beginning of the string is 'value_'
$("input[id*='value_']").each(function() {
    if ($(this).attr('id').startsWith('value_')) {
        // Do what you'd like with the matched input element, being $(this)
    }
});

EDIT: This might work better for you:

$("input[id^='value_']").each(function() {
    // Do what you'd like with the matched input element, being $(this)
});

Found here: https://stackoverflow.com/a/5413862/5169684

Community
  • 1
  • 1
Adam Mazzarella
  • 763
  • 1
  • 7
  • 14
  • Exactly what i needed. Thanks both. I must use by value_ because i have many field id with different name – user3808020 Oct 07 '15 at 19:58
  • Thanks a lot can You give me example to fill a field with $(this) – user3808020 Oct 07 '15 at 20:06
  • `$(this).val('Your value here');`. `this` references the current DOM object. `$(this)` selects the object using jQuery which allows you to manipulate it as your would any jQuery object. – Adam Mazzarella Oct 07 '15 at 20:32
  • Yes but $(this).val('Your value here'); fill fiill all field with same value. Really thanks a lot for Your answers, Can You give me example how to do that please? – user3808020 Oct 07 '15 at 20:39
  • What do you want to fill the value with? `$(this).val(Math.random());` will give each input element a separate value. `this` changes to the next element in the set each time the anonymous function in `each` completes. – Adam Mazzarella Oct 07 '15 at 20:42
  • $(this).val("somthing for first field"); this is for first field with id value_ ; then $(this).val("somthing for second founded field started with value_); not to be random. Sorry for my bad explanation – user3808020 Oct 07 '15 at 20:47
  • If you want to get the number associated with that ID, `var current_ID = $(this).attr('id').slice("value_".length)` will be what you're looking for. If you want to figure out what to do from there, you'll have to start a new thread as this is getting off topic. – Adam Mazzarella Oct 07 '15 at 20:55