17

I have this line of code in my page:

<div id='data'>...</div>      
<a id='addMore' href='#'>Add more </a>   

This line is actually in a bootstrap modal. I want that when a user clicks it, I want the div above it cloned. The issue is not the code for the cloning but that the click event is not even raised. In my .js file, I have this:

$('#addMore').click (...)

The ellipsis is for the code for preventing defaults and cloning. I tried testing with an alert. It still didn't work. I don't know why.

I discovered that if I added onClick='alert(...) in the tag, it worked.

Can anyone help?

This is the HTML of the modal (Wouldn't mind if anyone helped with formatting. I know it's a mess):

<div id="addEmailModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="addEmailLabel" aria-hidden="true">   
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times; </button> 
        <h3 id="addEmailLabel">Add Email</h3>
    </div>
    <div class="modal-body">
        <div id="emailData">
            <input type="text" placeholder="Name (optional)" class="input-xlarge" />
            <input type="email" placeholder="Email" />
        </div>
        <a id="addMore" href="#">Add more&hellip;</a>
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
        <button class="btn btn-primary" id="btnAdd">Add</button>
    </div>
</div>
CaptainBli
  • 4,121
  • 4
  • 39
  • 58
afaolek
  • 8,452
  • 13
  • 45
  • 60

7 Answers7

25
$(document).on('click', '#addMore', function(){...});

Modal is not ready with $(document).ready function. It becomes ready when it pops-up. This is the reason behind this.

Arnab
  • 4,216
  • 2
  • 28
  • 50
  • Could you add some details of why this should work? Code-only answers are not very useful and are subject to deletion. – Cristik Oct 29 '15 at 17:25
  • More details would be helpful. This approach corrected my problem though. – phenicie May 24 '16 at 15:47
  • 1
    I believe I know why this works, (it did correct my problem). By binding a click event on the document, every time you click the page anywhere, you refresh its DOM listing, which means that anything that was added after the document send the ready code is now available to `on()`. The middle argument is then used to specify the id/class that was added after `document.ready()` concluded. – Kyle L. Jul 06 '16 at 16:03
  • 1
    instead of document you could have used the modal Id like this : `$('#modal-Id').on('click', '#addMore', function(){...});` – Axel Sep 12 '16 at 15:44
  • Thanks for reminding me about this syntax. It's really weird, sometimes `$("button[id^='dismissDtModal_']").on('click', function(event) {...` works fine, and other times, I have to go with `$(document).on('click', "button[id^='dismissCmNotFound_'][id$='_Modal']", function (event) {...` I wish I had a better understanding of why, especially when both of those are used to handle a click of the 'close' button on BS Modal dialogs. – Scott Fraley Oct 03 '17 at 15:53
20

Try event delegation - jQuery's .on() method.

If the event is not working it may be because your tag <a id='addMore' href='#'>Add more </a> is added after the pages loads. You can catch the event with his parent container (note: the parent should already exist in the DOM when the pages loads). Try this:

$(document).ready( function () {
    //Search the parent class, id, or tag and then try to find the <a id="addMore"></a>  as a child
    $('.parent #addMore').on('click', function () {
        alert('addMore click event');
    });
    //Try too with the specific tag
    $('a#addMore').on('click', function () {
        alert('addMore click event');
    });
    //And try too with the specific tag
    $('a #addMore').on('click', function () {
        alert('addMore click event');
    });

});
cssyphus
  • 37,875
  • 18
  • 96
  • 111
Luis.Andrade
  • 318
  • 1
  • 9
  • This worked for me, ***although in my case the tag is not loaded after the page loads.*** Event delegation should not have solved my problem, but it did. *"Mine is not to reason why; mine is but to pry and pry." - Lynn Johnston* – cssyphus Dec 22 '15 at 16:02
  • You know, sometimes to find the solution to a problem we have to go deeper and deeper to find the reason. I glad that this solution help you. – Luis.Andrade Dec 22 '15 at 19:24
4

I hope this will helpful to you. I had the same issue, i just added modal ID in Jquery click event and it worked for me. Try this

$('#addEmailModal #addMore').click (...)
3

This isn't really an answer but it 'magically' resolved the issue. I moved the code for that page to another .js file and it worked. The former file was linked to many pages. I guess that was the source of problem. It seems strange though.

afaolek
  • 8,452
  • 13
  • 45
  • 60
2

My guess is that your element is added in the page after jQuery.ready event. Is your code in that block?

$( document ).ready( $('#addMore').click (...) )
2

I hope it's only a typo but you have a double quote before the id attribute, right after the a tag. In the first part of the code, it's ok though.

 <a" id="addMore" href="#">Add more&hellip;</a>

It can probably mess things up...

2

I had accidentally assigned the same id attribute to two different buttons in different modal dialogs. Changing this fixed the issue:

$(document).ready(function((){/*assign click to modal dialog buttons...

This worked just as expected.

halfer
  • 19,824
  • 17
  • 99
  • 186
DroidOS
  • 8,530
  • 16
  • 99
  • 171