[ACCEPTED]-jQuery selection with dynamic id's-jquery

Accepted answer
Score: 37
$("input[id^='id_airline_for_']").each(function() {
  var id = parseInt(this.id.replace("id_airline_for_", ""), 10);
  var airline = $("#id_airline_for_" + id);
  var flightNumber = $("#id_flight_number_for_" + id);
  // do stuff with airline and flightNumber <input>s
});

0

Score: 4

You can use $("input#id_airline_for" + id) (where id is your number, e.g., 8), but 2 it will be faster if you drop the tag name 1 and just use:

$("#id_airline_for" + id);
Score: 2

The following will get you all of the inputs 4 that contain id_airline_for in their name.

$("input[id^='id_airline_for']")

If you need 3 to do this on a per form basis, you'll want 2 to give each form its own ID and select 1 them in groups that way.

$("#formJetBlue input[id^='id_airline_for']")
$("#formNwa input[id^='id_airline_for']")
$("#formOceanic input[id^='id_airline_for']")
Score: 2

I don't think you should be using the @ sign 4 and you're missing some quotes:

Instead of 3 this: $('input[id^=@id_airline_for_]')

try 2 this: $("input[id^='id_airline_for_']")

See 1 this, too:

http://docs.jquery.com/Selectors/attributeStartsWith#attributevalue

Score: 0

Selects elements that have the specified 4 attribute with a value containing the a 3 given substring

$('input[id *= id_airline_for]').attr('checked', headerChecked);

it will select all elements 2 that contain 'id_airline_for' string in 1 the 'id' attribute

More Related questions