JavaScript Cant select ids that I loaded with jQuery cant select plane game recipe

I understand that you need to use ".on" to use code that you loaded with jquery after the page has loaded. (At least I think it works that way)

So I tried that but it somehow just doesn't do a thing at all. No errors in the console either.

$("#forgot_password").click(function(){
  var forgot_password = '<div id="toLogin" style="cursor:pointer;">Prijava</div>'
  $("#loginPopupForm").html(forgot_password);                                       
});

$("#toLogin").on("click", function(){
    alert("Hello");
});

So when I click on #forgot_password it does execute the first click function. But when I click on #toLogin it doesn't do anything and I think its because its loaded with jquery when I click on #forgot_password

Answer:1

You need to bind to an element that is present when the page loads, like body for example. Just change your code to what is shown below

$("body").on("click", "#forgot_password", function(){
  var forgot_password = '<div id="toLogin" style="cursor:pointer;">Prijava</div>'
  $("#loginPopupForm").html(forgot_password);                                       
});

$("body").on("click", "#toLogin", function(){
    alert("Hello");
});
Answer:2

You are setting the on to the wrong thing. You want it to be:

$(document).on('click', '#toLogin', function() {alert('hello') });

The id isn't there until you do the other click event, so jQuery is not finding any element to set the click event on. You need to have an element that has been rendered in the DOM to set the event on.

Answer:3

You are totally right about the problem : on() targets only elements that are already existing as it runs. What you need in jQuery is called Delegated event and is well explained on the Jquery doc page. The difference in the code is thin, but it's how you're supposed to do.

Answer:4

I got two browser windows on the same domain, one is the main window and the second is small popup window. i found this: How to Communicate between two browser windows? but, the problem is the ...

I got two browser windows on the same domain, one is the main window and the second is small popup window. i found this: How to Communicate between two browser windows? but, the problem is the ...

  1. communicate between popup window
  2. communicate with popup window
  3. javascript communicate between popup
  4. chrome extension communicate between popup and background

I have used SelectBoxIt plugin to display the select boxes in my application. I have two select boxes, if I change the value in first select box, another should automatically update (it should ...

I have used SelectBoxIt plugin to display the select boxes in my application. I have two select boxes, if I change the value in first select box, another should automatically update (it should ...

Essentially, what I am saying is I want to create a tag or label that appears on top of/on the surface of an object so that the tag always faces the camera when the user clicks the object even when ...

Essentially, what I am saying is I want to create a tag or label that appears on top of/on the surface of an object so that the tag always faces the camera when the user clicks the object even when ...

I am trying to bind data to angular template. But I'm facing the following issue. I am using $templateCache to retrieve the template but I cannot find data into the template. $templateCache.get('q1....

I am trying to bind data to angular template. But I'm facing the following issue. I am using $templateCache to retrieve the template but I cannot find data into the template. $templateCache.get('q1....

  1. binding data template
  2. data-bind template knockout
  3. data-bindu003d template name
  4. data-bind template javascript
  5. data-bind template not working
  6. data-bind template foreach
  7. data binding datatemplate wpf
  8. bind data to template column in gridview
  9. wpf datatemplate binding
  10. kendo data bind template
  11. bind data to item template gridview asp.net
  12. div data-bindu003d template
  13. ko data-bind template
  14. data-bind jquery template
  15. knockoutjs data-bind template
  16. html data bind template
  17. data bind polymer template
  18. durandal data bind template
  19. vue bind data to template
  20. kendo grid data-bind template