Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alert not showing #9

Closed
football-fastball opened this issue Apr 23, 2015 · 4 comments
Closed

alert not showing #9

football-fastball opened this issue Apr 23, 2015 · 4 comments

Comments

@football-fastball
Copy link

Nice library indeed!
Just noticed this issue, the code below is not showing the alert as its should show the alert from the function of the click event of the button.

$(document).ready(function() {
      $( 'body' ).append( "<input type='button' id='alertdiv' value='Text of Alert Button' style='cursor:pointer' />" );
      $('#alertdiv').click(function() {alert("Hello, world!");});
});
@pablomayobre
Copy link

I think this wouldnt work with jQuery either, since the #alertdiv element may not be ready when you try to apply the click handler, try doing something like:

$(document).ready(function() {
      $('body').append("<input type='button' id='alertdiv' value='Text of Alert Button' style='cursor:pointer' />");
      $('body').click(function(event) {
            if ($(event.target).is('#alertdiv')){
                  alert("Hello, world!");
            }
      });
});

Since body will be ready on document.ready you can attach handlers to it, if onclick is triggered, it will change if the event.target is the alertdiv button, and if it is make an alert pop-up

PS: In jQuery you have the advantage with the selector attribute for on so you could do it with a simpler code:

$(document).ready(function() {
      $('body').append("<input type='button' id='alertdiv' value='Text of Alert Button' style='cursor:pointer' />");
      $('body').on('click', '#alertdiv', function(event) {
            alert("Hello, world!");
      });
});

@football-fastball
Copy link
Author

I have tested that jQuery 2.1.3 does run both your code examples as well as the example I provided.

@albatros2ko
Copy link

Isn't there a .trigger(...) function? Why use .click(...)?

This one works for me (tested on both Chrome and Firefox). And .click(..); isn't defined or at least, implemented yet.

$(document).ready(function () {
  $("body").append('<input id="alertdiv" type="button" value="Text of Alert Button" style="cursor:pointer" />');
  $("#alertdiv").on("click", function () { alert("Hello!"); });
});

@bendc
Copy link
Owner

bendc commented Apr 23, 2015

@vkeusebio is right, Sprint doesn't support shorthand methods like .click(), you should use the generic .on() instead.

@bendc bendc closed this as completed Apr 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants