Avatar of APD Toronto
APD Toronto
Flag for Canada

asked on 

Which button is clicked?

I had this question after viewing Duplicating DIVs and nested elements.

Building on Julian's answer for cloning my DIV, I also added a Clear File button, as follows
            <div id="divAttachNew">
                
                <label class="lblAutoWidth">
                    <span class="fa fa-paperclip"></span>
                    Attach New Files</label>
                <span>(maximum file size: <?php echo $env['upload_size_mb']; ?> MB)</span>
                
                <div id="divAttachFiles">
                                        
                    <input type="file" id="file[]" name="file[]" class="attachFile">                    
                    <input type="button" class="btnClearFile" 
                           id="btnClearFile[]" name="btnClearFile[]" value="Clear File">
                    
                </div>

            </div>            


        <div id="divAttachAdditional">
            <span class="fa fa-plus-circle"></span>
            <a id="aAddFiles">Attach Additional files</a>
        </div>

Open in new window


And my jQuery is
    $('#aAddFiles').on('click', function(e){
        e.preventDefault();
        $('#divAttachNew').append($('#divAttachFiles').clone());
    });
      
    $('.btnClearFile').on('click', function(){
       
        console.log($(this));
        
    });

Open in new window


So now I have 2 questions.

1- When clicking #btnClearFile, only the first button produce output, the others do not. Why?

2- How  would I determine which  of thhese buttons is clicked, and would remove that corresponding DIV?

Thank you.
JavaScriptjQuery

Avatar of undefined
Last Comment
Jan Louwerens

8/22/2022 - Mon