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 id="divAttachAdditional">
            <span class="fa fa-plus-circle"></span>
            <a id="aAddFiles">Attach Additional files</a>

Open in new window

And my jQuery is
    $('#aAddFiles').on('click', function(e){
    $('.btnClearFile').on('click', function(){

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.
APD TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jan LouwerensSoftware EngineerCommented:
Html element ids should be unique within the DOM. If your duplicating sections, you should probably be using classes instead of ids, to prevent duplicate ids.

The reason only the first one is producing output is because that's the only one in that exists in the DOM at the time the click event is bound. If you want to be able to target dynamically generated elements, you'll need to use a diffferent form of 'on'. try:

$(body).on('click', '.btnClearFile', function(e){
        console.log('event.target: %o', e.target);

Open in new window

Either 'this' or the event target will be the element that was clicked. From there, you can use jQuery to find the nearest parent (or ancestor) that you want to remove from the DOM.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.