This is quite frustrating. My code was working fine until last week. I am adding multiple textboxes to my HTML page when the user changes [using change()] the value in a dropdown selection.
Here's the HTML code snippet:
<div id="files" class="control-group">
<label class="control-label">No. of files</label>
<div class="controls" >
<select id="files" name="files" class="span3">
<option value="">--Select Option--</option>
<?php for($i=1;$i<21;$i++){ ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option> <?php } ?>
</select>
</div>
</div>
<div class="control-group" id="titles">
<label class="control-label">File Titles</label>
<div class="controls" id="titleAdd"></div>
</div>
Here's my Javascript / jQuery:
$(document).ready(function(){
$("#titles").hide();
});
var container;
// Add & Remove textboxes
$("#files").change(function(){
//Remove all textboxes
$("#titles").show();
$(container).empty();
$(container).remove();
//"DIV" ELEMENT AND DESIGN IT USING JQUERY ".css()" CLASS.
container = $('<div>', {class: 'controls'});
var option = $("#files").val();
for(i=1;i<=option;i++)
{
// Add a TextBox
$(container).append('<input style="display: block;" type=text name="Description[]" class="span3 input-left-top-margins" id="Description' + i +'"' + 'placeholder="File ' + i + ' Title" />');
}
$('#titleAdd').after(container); // ADD THE DIV ELEMENT IN THE RIGHT PLACE.
});
The most irritating part is that this code was working fine a few days ago.
containerwould be undefined at start. Also, your append will duplicate theid="Description'#files(say 5), 5 textboxes used to get created. Now, the textboxes aren't getting created