3

Working on the jquery clone with the current code if I click add more button I am able to clone in the original one i can able to attach the file but if I try to attach any file in the cloned one it is not working.

I tried even replacing id with class but still it is not working

Here is the jquery code

    var count=0;
    $(document).on("click", ".attch_add_button", function () {
        var $clone = $('.cloned-row4:eq(0)').clone(true,true);
        //$(".custom-file-input").closest('form').trigger('reset');
        //$(".custom-file-input").replaceWith('.check'.val('').clone(true));
        //alert("Clone number" + clone);
        $clone.find('[id]').each(function(){this.id+=''});
        $clone.find('.btn_more').after("<input type='button' class='btn_right btn_less1' id='buttonless'/>")
        $clone.attr('id', "added"+(++count));
        //$clone.find('#custom-file-input').val('');
        $clone.find('.preview').hide();
        $clone.wrap('<form>');
        $clone.closest('form').trigger('reset');
        $clone.unwrap();

        //$clone.find('input[type=file]').val('');

        //$clone.find('.pn_field').val("");
        $(this).parents('.medica_rpt').after($clone);
    });

Here is the fiddle Link

Kindly suggest here

7
  • Click to the right of the 'Attach' button and you'll notice the input is misplaced. You would have to do a deep clone by the way to copy the event listeners. Commented Sep 17, 2015 at 3:21
  • hi thanks for the suggestion I have tired with true for the clone but not working can you please help me Commented Sep 17, 2015 at 5:11
  • hi kindly please suggest me Commented Sep 17, 2015 at 5:56
  • Seems to work alright when using clone(true, true). Apart from the input itself being offset to the right from the button in the fiddle (seems to be in place when pasted in Codepen). I'd be very careful with any kind of file uploads by the way. codepen.io/anon/pen/RWaRzz Commented Sep 17, 2015 at 6:01
  • @hi thanks for the reply first time when the user click the attach button he can able to attach once when the user click the add more then he tries the attach button from the cloned div it was not working kindly please suggest me Commented Sep 17, 2015 at 8:14

1 Answer 1

0

You need to attach unique events for each element, if you create a function that attaches the events to a received element the problem will be solved:

function addActions(container){

    var $preview = container.find(".preview");
    //var $acceptdiv = $("#accept_div");
    //$acceptdiv.hide();
    $preview.hide();

    container.find(".check").on("change", function(){
      alert("alert");
      var filename = this.value;
      var files = this.files;
      var URL = window.URL||window.webkitURL;
      var url = URL.createObjectURL(files[0]);
      $preview.attr("href", url);
      $preview.show();
      //$acceptdiv.show();
      container.find("#file_name").val(filename).prop("disabled", true);
    });

  }

I've modified your example:

jsfiddle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.