AbhiJA
AbhiJA

Reputation: 341

How to add more than one time to a div using jquery?

I have the below code and when the "Browse" click, i want place the same content in the div "projectidAppe" and change the "Browse" to "Delete" button . i have done this using the following jquery,

$(document).on("change", "#idProjectTitle", function(e) {
  var datatoappend = ' <div class="form-group file-uploader"><div class="input-group col-xs-12"><span class="input-group-addon"><i class="glyphicon glyphicon-picture"></i></span><input type="text" id="txtProjectTitle" style="height: 35px !important" class="form-control input-lg" disabled placeholder="Upload Image"><div class="input-group-btn"><div class="browse btn btn-primary"><i class="glyphicon glyphicon-search"></i> Browse<input type="file" accept="image/*" id="idProjectTitle" multiple="multiple" name="fileUploadphoto" class="file"></div></div></div></div>';
  $("#txtProjectTitle").attr('placeholder', $(this).val().split('\\').pop());
  var btnDelete = '<div id="idImgDelete" class="browse btn btn-primary">Delete</div>';
  $('#clearbtn').html('');
  $('#clearbtn').append(btnDelete);
  $("#projectidAppe").append(datatoappend);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="exampleInputEmail1">Project Photos</label>
<div class="form-group file-uploader">
  <div class="input-group col-xs-12">
    <span class="input-group-addon">
      <i class="glyphicon glyphicon-picture"></i>
    </span>
    <input type="text" id="txtProjectTitle" style="height: 35px !important" class="form-control input-lg" disabled placeholder="Upload Image">
    <div id="clearbtn" class="input-group-btn">
      <div class="browse btn btn-primary">
        <i class="glyphicon glyphicon-search"></i> Browse
        <input type="file" accept="image/*" id="idProjectTitle" multiple="multiple" name="fileUploadphoto" class="file">
      </div>
    </div>
  </div>
</div>

But now, not happening each image name inside the txtProjectTitle 's placeholder respectively. After file upload, how can I show delete button for each one and how to delete one row when click on the delete button?

Upvotes: 2

Views: 55

Answers (1)

Sahal Tariq
Sahal Tariq

Reputation: 198

Your Html:

    <div id="projectidAppe">
<label for="exampleInputEmail1">Project Photos</label>
<div class="form-group file-uploader">
  <div class="input-group col-xs-12">
    <span class="input-group-addon">
      <i class="glyphicon glyphicon-picture"></i>
    </span>
    <input type="text" id="txtProjectTitle-0" style="height: 35px !important" class="form-control input-lg" disabled placeholder="Upload Image">
    <div id="clearbtn-0" class="input-group-btn">
      <div class="browse btn btn-primary">
        <i class="glyphicon glyphicon-search"></i> Browse
        <input type="file" accept="image/*" class="idProjectTitle" multiple="multiple" name="fileUploadphoto" class="file">
      </div>
    </div>
  </div>
</div>
</div>
</div>

Your JS:

    var count = 1;
$(document).on("change", ".idProjectTitle", function(e) {
  var datatoappend = ' <div class="form-group file-uploader"><div class="input-group col-xs-12"><span class="input-group-addon"><i class="glyphicon glyphicon-picture"></i></span><input type="text" id="txtProjectTitle-'+count+'" style="height: 35px !important" class="form-control input-lg" disabled placeholder="Upload Image"><div id="clearbtn-'+count+'" class="input-group-btn"><div class="browse btn btn-primary"><i class="glyphicon glyphicon-search"></i> Browse<input type="file" accept="image/*" class="idProjectTitle" multiple="multiple" name="fileUploadphoto" class="file"></div></div></div></div>';
  $("#txtProjectTitle-"+(count-1)).attr('placeholder', $(this).val().split('\\').pop());
  var btnDelete = '<div id="idImgDelete" class="browse btn btn-primary">Delete</div>';
  $('#clearbtn-'+(count-1)+' .browse').css('display','none');
  $('#clearbtn-'+(count-1)).append(btnDelete);
  $("#projectidAppe").append(datatoappend);
  count ++;
});
$(document).on("click", "#idImgDelete",function(){
    $(this).parentsUntil(".form-group.file-uploader").remove();
});

Upvotes: 1

Related Questions