I have a loop that when it is called, creates a div with some form elements in them. Each div is based on a variable, "i" to give unique names to the fields and divs. Is there a way I can store what the variable was at the point of creating the div?
For example, div1 is created and everything in it has 1 (the variable) attached to the name. The form elements rely on each other and are called by ID. Problem is, when I create a new div and the variable (i) is changed to 2, the first set of form elements try to use 2 instead of 1.
Make sense?
Edit: Here's some code. It's pretty messy so I apologize in advance.
var i = 0;
$('a#add-product').click(function(event){
i++;
$('<div />').addClass('product').attr('id', 'product'+i)
.append($('<h2><img src="<?php echo base_url();?>img/product.png" alt="" />Product '+i+'</h2>'))
.append($('<div class="info-line"><label>Division</label><p><select id="selection-'+i+'" class="selection"><option value="">- Select a Division -</option><option value="abrasives">Abrasives</option><option value="tapes">Bonding, Surface Protection & Tapes</option><option value="packaging">Packaging</option></select></p></div>'))
.append($('<div class="info-line"><label>Category</label><p><select id="selectionresult-'+i+'" name="selectionresult-'+i+'" class="selectionresult"></select><span id="result-'+i+'" class="result"> </span></p></div>'))
.append($('<div class="info-line"><label>Product</label><p><select id="selectionresult2-'+i+'" name="selectionresult2-'+i+'" class="selectionresult2"></select><span id="result2-'+i+'" class="result2"> </span></p></div>'))
.append($('<a class="remove" href="#add-product" id="remove-product'+i+'"><img src="<?php echo base_url();?>img/remove-product.jpg" alt="" />Remove Product</a>'))
.appendTo("#products");
// START OF ADDITIONAL PRODUCT DROP DOWNS
$("#selectionresult-"+i).hide();
$("#selectionresult2-"+i).hide();
$("#selection-"+i).change( function() {
$(this).next(".selectionresult").hide();
$(this).next(".selectionresult2").hide();
$("#result-"+i).html('Retrieving ...');
$.ajax({
type: "POST",
data: "data=" + $(this).val(),
url: "<?php echo base_url();?>dropdown.php",
success: function(msg){
if (msg != ''){
$("#selectionresult-"+i).html(msg).show();
$("#result-"+i).html('');
}
else{
$("#result-"+i).html('<em>No item result</em>');
}
}
});
});
$("#selectionresult-"+i).change( function() {
$(this).next(".selectionresult2").hide();
$("#result2-"+i).html('Retrieving ...');
$.ajax({
type: "POST",
data: "data=" + $(this).val(),
url: "<?php echo base_url();?>dropdown.php",
success: function(msg){
if (msg != ''){
$("#selectionresult2-"+i).html(msg).show();
$("#result2-"+i).html('');
}
else{
$("#result2-"+i).html('<em>No item result</em>');
}
}
});
});
});