In following segment refers how to create dynamically add multiple files uploading fields in forms.
Copy and paste following code segment into html file. Then open it in a browser and try out the demo. (I have used the on-line reference for the jquery-1.10.1.min.js , else download it from http://jquery.com/download/)
<html>
<script src="http://code.jquery.com/jquery-1.10.1.min.js">
/**
* Above is for on-line reference for jQuery.
* If above is not worked, please download the jquery-1.10.1.min.js from here. http://jquery.com/download/
*
*/
</script>
<script type="text/javascript" xmlns="http://www.w3.org/1999/html">
$(document).ready(function () {
var counter = 2;
$("#addButton").click(function () {
if (counter > 50) {
alert("Only 50 files allow...");
return false;
}
var divId = 'TextBoxDiv' + counter;
var newTextBoxDiv = $(document.createElement('div'))
.attr("id", divId);
newTextBoxDiv.after().html('<input type="file" name="filesUpload" id="filesUpload' + counter + '">' +
'<input type="button" id="removeButton" value="x" class="btn btn-small btn-danger" onclick="removeFile(\'' + divId + '\')"/>'
);
newTextBoxDiv.appendTo("#TextBoxesGroup");
counter++;
});
$("#removeButton").click(function () {
if (counter == 1) {
alert("No more files to remove...");
return false;
}
for (i = 1; i < counter; i++) {
$("#TextBoxDiv" + i).remove();
}
counter = 1;
});
});
function removeFile(divId) {
var divGroup = document.getElementById('TextBoxesGroup');
var fileDiv = document.getElementById(divId);
divGroup.removeChild(fileDiv);
}
</script>
<body>
<div class="row-fluid">
<div class="span4" id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<input type="file" name="filesUpload" id="filesUpload1">
<input type="button" class="btn btn-small btn-danger" value="x" onclick="removeFile('TextBoxDiv1')"/>
</div>
</div>
<div class="span4">
<input type="button" id="addButton" value="Add more"/>
<input type="button" id="removeButton" value="Remove all"/>
</div>
</div>
</body>
</html>
Note : We can replace text fields instead of above input type "file"
Copy and paste following code segment into html file. Then open it in a browser and try out the demo. (I have used the on-line reference for the jquery-1.10.1.min.js , else download it from http://jquery.com/download/)
<html>
<script src="http://code.jquery.com/jquery-1.10.1.min.js">
/**
* Above is for on-line reference for jQuery.
* If above is not worked, please download the jquery-1.10.1.min.js from here. http://jquery.com/download/
*
*/
</script>
<script type="text/javascript" xmlns="http://www.w3.org/1999/html">
$(document).ready(function () {
var counter = 2;
$("#addButton").click(function () {
if (counter > 50) {
alert("Only 50 files allow...");
return false;
}
var divId = 'TextBoxDiv' + counter;
var newTextBoxDiv = $(document.createElement('div'))
.attr("id", divId);
newTextBoxDiv.after().html('<input type="file" name="filesUpload" id="filesUpload' + counter + '">' +
'<input type="button" id="removeButton" value="x" class="btn btn-small btn-danger" onclick="removeFile(\'' + divId + '\')"/>'
);
newTextBoxDiv.appendTo("#TextBoxesGroup");
counter++;
});
$("#removeButton").click(function () {
if (counter == 1) {
alert("No more files to remove...");
return false;
}
for (i = 1; i < counter; i++) {
$("#TextBoxDiv" + i).remove();
}
counter = 1;
});
});
function removeFile(divId) {
var divGroup = document.getElementById('TextBoxesGroup');
var fileDiv = document.getElementById(divId);
divGroup.removeChild(fileDiv);
}
</script>
<body>
<div class="row-fluid">
<div class="span4" id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<input type="file" name="filesUpload" id="filesUpload1">
<input type="button" class="btn btn-small btn-danger" value="x" onclick="removeFile('TextBoxDiv1')"/>
</div>
</div>
<div class="span4">
<input type="button" id="addButton" value="Add more"/>
<input type="button" id="removeButton" value="Remove all"/>
</div>
</div>
</body>
</html>
Note : We can replace text fields instead of above input type "file"
Comments
Post a Comment