2

I'm trying to select the id's of dynamic input fields in my code. When clicking a button, the form will create a form field like this:

<td><input type="text" id="field_a_1"></td>
<td><input type="text" id="field_b_1"></td>
<td><input type="text" id="field_c_1"></td>

When I click on the button again I get this:

<td><input type="text" id="field_a_2"></td>
<td><input type="text" id="field_b_2"></td>
<td><input type="text" id="field_c_2"></td>

What I want to do is select only the field id that I need to pull the value from that particular input and pass it to a variable like this:

var example = $(":input:eq(0)").val();

I know that by adding the :eq(0) after the :input selector it will only grab the id for field_a_1, so how do I set it up so that I can pull just the field that I need to assign it to a variable?

3
  • 2
    Your question is slightly confusing... do your inputs change dynamically with a button push? Are you trying to target only the 1st, 2nd, etc input field after each button push? Commented May 21, 2010 at 13:13
  • Perhaps you could reword the statements just a bit to clarify your desired "pull just the field that I need to assign it to a variable" and "select only the field id that I need to pull the value from that particular input" cause me to wonder a bit. Commented May 21, 2010 at 13:17
  • fudgy, what I meant to say is that when I click on the button again I get a new set of inputs with new id's. The first set in the example represents a single table row. The second set is actually a new table row and does not alter the first row. So as I click the button I will get a new table row that will increment the number in the id names. What I needed was to be able to calculate the values of each row individually, therefore needing to be able to select the correct id's for each row. You'll have to forgive me since I'm new to jquery. Commented May 21, 2010 at 17:37

3 Answers 3

1

That is what the ID is for. So you can single out a particular element.

$('#field_b_2').val();   // Will return the element with that ID.

The # indicates that you are looking for an element with an ID, as opposed to . which would look for an element (or elements) with a class:

$('.someClass').val();   // Will return elements with that class

Please remember that IDs may not be shared among elements. Only one element may have a particular ID.

Classes, on the other hand, can be shared among as many elements as you need.

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

1 Comment

+1 - This is probably what he's looking for with the class, but I can't really tell from the question.
0

If you want only the id of the element you should do:

var example = $(":input:eq(0)").attr("id");

And then you can access the field again later with:

$("#" + example).show();    //or whatever you want to do

3 Comments

You can just do var example = $(":input").attr("id"); :)
but he needs to select a specific one with an index. If he needs anything but the first one, your code wouldn't work.
Since you're not passing a variable into :eq(0) that's beside the point :) But yes, what I posted is just a quicker shortcut for your specific posted code.
0

your Input Ids keep changing like : When you click first time it will All: field_a_1 then , field_a_2, etc.

you simply using this to getting the correct values.

$('input[id^=field_]').each(function(){
   alert($(this).val());
});

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.