0

I have the following problem:

I have a button like: <button id="button_show_countries" >Start</button> in the index.html and a click event in the view.js:

        $("button#button_show_countries").click(function() {
            debug('View Click Event triggered: notice the Controller that the user wants to see the Interests data');
            Controller().countrieslandscapeasked();
        });

This wokrs very well. Now I create with the view a button. This button I add to index.html. The button looks like:

   <button class="insertButton" type="button" id="insert_button_0">insert here</button>

and in the view is also an eventlistener for this:

$("#insert_button_0").click(function() {
            debug('Data should be add at Position 0');

        });

But when I click on the button the event is not "fired". What is wrong with my code? :/ The button work, if I add him at the beginning like the show countries button. Is it problematic that I add the button dynamically at runtime?

Thanks for help.

nero
  • 27
  • 3
  • 1
    The `click()` binds event on the elements which are present in the DOM. Use event delegation `$(document).on('click', '#insert_button_0', function() {`. Instead of using `document` as target, use static parent. – Tushar Jan 02 '16 at 13:27
  • sorry, I do not really know what you mean. Where I should add this line? I work, with a MVC, so I can not call the method directly in the index.html or? – nero Jan 02 '16 at 13:34
  • First of all, read the answers on the above link, and you'll understand. You need to use the `$(document).on('click', '#insert_button_0', function() {` in place of `$("#insert_button_0").click(function() {`. – Tushar Jan 02 '16 at 13:36
  • sorry I get it. I must add this line in the view... thanks a lot – nero Jan 02 '16 at 13:36
  • sorry, but I had another question which is I thin neraly the same. I had add also dynamically an selector. Now I would get when I press on the button insert_button_0 the selected element. I tryed it with: var selected = $("select#not_added").find('option:selected'); But it does not work, i alway get that selected is undifined – nero Jan 02 '16 at 14:47

0 Answers0