troubleshooting Question

Which button is clicked?

Avatar of APD Toronto
APD TorontoFlag for Canada asked on
JavaScriptjQuery
1 Comment1 Solution110 ViewsLast Modified:
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>

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

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.
ASKER CERTIFIED SOLUTION
Jan Louwerens
Backend Team Lead

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros