• Hi,
    I have created some dynamically add/remove fields. All fields are working fine except the image upload field.

    The proble is that when I add image in first field it displays fine. But when I add image on second or more fields the image gets display on first field only.

    Means every time when I add image to fields it only display preview in first field only. I want it to get display on their respective fields.

    Here is my code:

    <?php get_header(); ?>
    
    <form method="POST" enctype="multipart/form-data">
    
      <div class="panel panel-default">
    
        <div class="panel-heading">
          <center><b>Team Members</b></center>
        </div>
    
        <div class="panel-body">
    
          <div class="row">
    
            <div class="col-md-4">
    
              <div class="thumbnail">
    
                <img src="" id="member_image" alt="">
    
              </div>
    
              <div class="form-group">
    
                <label class="btn btn-success btn-block btn-file">Add Image<input type="file" name="member_image[]" onchange="preview_member(event)" style="display: none;"></label>
    
              </div>
    
            </div>
    
            <div class="col-md-8">
    
              <div class="form-group">
                <label for="member_name">Member Name <b style="color:#FF0000;">*</b></label>
                <input type="text" class="form-control" name="member_name[]" placeholder="">
              </div>
    
              <div class="form-group">
                <label for="member_role">Role in Project <b style="color:#FF0000;">*</b></label>
                <input type="text" class="form-control" name="member_role[]" placeholder="">
              </div>
    
              <div class="form-group">
                <label for="member_email">Email address <b style="color:#FF0000;">*</b></label>
                <input type="email" class="form-control" name="member_email[]" placeholder="">
              </div>
    
              <div class="form-group">
                <label for="member_facebook_id">Facebook Username <b style="color:#FF0000;">*</b></label>
                <input type="text" class="form-control" name="member_facebook_id[]" placeholder="">
              </div>
    
            </div>
    
          </div>
    
          <button type="button" class="btn btn-success btn-block" id="add-member-fields"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Member</button>
    
          <br>
    
          <div id="member-fields">
    
          </div>
    
        </div>
    
      </div>
    
    </form>
    
    <script type='text/javascript'>
    
    jQuery(document).ready(function($) {
    
      //fadeout selected item and remove
      $(document).on('click', '#remove-member-fields', function(event) {
        event.preventDefault();
        $(this).parent().fadeOut(300, function() {
          $(this).empty();
          return false;
        });
      });
    
      var rows = '<div class="team-member-fields"><div class="row"><div class="col-md-4"><div class="thumbnail"><img src="<?php echo esc_url( site_url(' / profile - images / blank - image.png ') ); ?>" alt=""></div><div class="form-group"><label class="btn btn-success btn-block btn-file">Add Image<input type="file" name="member_image[]" onchange="preview_member(event)" style="display: none;"></label></div></div><div class="col-md-8"><div class="form-group"><label for="member_name">Member Name <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_name[]" placeholder=""></div><div class="form-group"><label for="member_role">Role in Project <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_role[]" placeholder=""></div><div class="form-group"><label for="member_email">Email address <b style="color:#FF0000;">*</b></label><input type="email" class="form-control" name="member_email[]" placeholder=""></div><div class="form-group"><label for="member_facebook_id">Facebook Username <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_facebook_id[]" placeholder=""></div></div></div><button type="button" class="btn btn-danger btn-block" id="remove-member-fields"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove Member</button><br></div></div></div>';
    
      //add input
      $('#add-member-fields').click(function() {
        $(rows).fadeIn("slow").appendTo('#member-fields');
        i++;
        return false;
      });
    
    });
    
    function preview_member(event) {
    
      var reader = new FileReader();
    
      reader.onload = function() {
    
        var output = document.getElementById('member_image');
        output.src = reader.result;
    
      }
    
      reader.readAsDataURL(event.target.files[0]);
    
    }
    
    </script>
    
    <?php get_footer(); ?>

    Plz help… Thanks…

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Mineshrai

    (@mineshrai)

    @techdancer thanks… I think the problem is with image id.. But I am unable to modify the code… Plz help..

    Moderator bcworkz

    (@bcworkz)

    You are correct, the problem is the image ID. Your preview_member() function places the result of whatever field changed into the first preview field with ID ‘member_image’. It does not matter how many containers have ID ‘member_image’, only the first will be used because there is supposed to be only one element with a given ID on any given page. Script does not try to find all instances of an ID, it stops looking on the first found because there should not be any more.

    Since you are using jQuery, you can select elements by a specific class attribute and rely on this in script to reference the element clicked on. (assuming we’re within a .click() closure. Similar logic for any event closure) If need be, you can use parent and child methods to get from this to whatever container you need that’s relative to the clicked element.

    This means you can have multiple elements selected by a class attribute, but only one of those will have an event triggered at any one time, so this will refer specifically to that one element regardless how many elements were selected by the jQuery class selector.

    Never attempt to assign or use a specific ID for more than one element on a page. It’s invalid HTML and scripts will not work as expected.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Image preview not working on other Dynamically created File fields’ is closed to new replies.