Ok this might be a bit confusing, but here goes. Let's say I have a a few select dropdowns on a page.
<select id="filter" name="filter[]">
<option value="">-- Select Filter --</option>
</select>
<select id="load_choice" name="load_choice[]">
<option value="">-- Select Load_choice --</option>
</select>
<select id="plastic" name="plastic[]">
<option value="">-- Select Plastic --</option>
</select>
These are dynamically filled from a database with an ajax request. Each set of select options are dependent on the previous selection. This is just a snippet of all the select dropdowns, but essentially their selections creates a "product". Here is the javascript that connects to the php (which connects to the DB).
$(document).ready(function() {
$('#filter').change(function(){
$('#load_choice').fadeOut();
$('#loader').show();
$.post("ajax/ajax_load_choice.php", {
country: $('#country').val(),
filter: $('#filter').val()
}, function(response){
setTimeout("finishAjax('load_choice', '"+escape(response)+"')", 400);
});
return false;
});
$('#load_choice').change(function(){
$('#plastic').fadeOut();
$('#loader').show();
$.post("ajax/ajax_plastic.php", {
country: $('#country').val(),
filter: $('#filter').val(),
load_choice: $('#load_choice').val()
}, function(response){
setTimeout("finishAjax('plastic', '"+escape(response)+"')", 400);
});
return false;
});
$('#plastic').change(function(){
$('#UG_tipping').fadeOut();
$('#loader').show();
$.post("ajax/ajax_UG.php", {
country: $('#country').val(),
filter: $('#filter').val(),
load_choice: $('#load_choice').val(),
plastic: $('#plastic').val()
}, function(response){
setTimeout("finishAjax('UG_tipping', '"+escape(response)+"')", 400);
});
return false;
});
});
function finishAjax(id, response){
$('#loader').hide();
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
}
}
NOW, let's say I want to add another of the exact same form with the exact same select options in order to "create another product" on the same page (hence the array on the NAME tag for each select). Since the form is dependent on unique IDs, how could I make the IDs in this chunk of code dynamic?
$('#filter').change(function(){
$('#load_choice').fadeOut();
$('#loader').show();
$.post("ajax/ajax_load_choice.php", {
country: $('#country').val(),
filter: $('#filter').val()
}, function(response){
setTimeout("finishAjax('load_choice', '"+escape(response)+"')", 400);
});
return false;
});
I will eventually have 5 sets of those select groups so the user can create 5 products. How would I make it so I don't have to do id="filter1" and id="filter2" to coincide with $('#filter').change(function....blah blah blah. Basically, can I make ID's in a jquery function dynamic?