Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts

    should I return false? and what happened to the click?

    hello,

    I'm working on a greasemonkey script that has a form (that isn't really a form, but just a collection of inputs - otherwise I guess I would be using beforesubmit) which is submitted somehow (dunno how, hoping not to have to find out) on the click of a button.

    What I want to do is do a validation on the "form" before it gets sent. I struggled for a while because I was adding a click listener to the button (making the validation happen after the info was sent), but I realised that using "mousedown" I get to sneak in, and do the validation. I then just programatically click the submit button and away it goes.

    Which is great. Works fine on my machine, anyway. But I have two doubts. A validation error brings up a confirm. If you click OK, the form sends. If you click cancel it doesn't. But shouldn't there be a return false in there somewhere, even though you would be returning false from the mousedown which I guess is unrelated to the click?

    And that's the second question: The way I understand it, the event order is mousedown, click, mouseup. So what happens to the click event? Does it die while the confirm box is showing?

    Here's a very basic example which demonstrates this. It's in jQuery because the page uses jQuery and I have to attach listeners to the dynamically created button (which I have no control over) - I know I can do that in vanilla, but it's just easier this way. But anyway, this isn't a jQuery question - the jQuery works fine. What I'm asking is how the javascript works here. More than anything, why don't I need to be canceling the click event in some way?

    Any thoughts much appreciated.

    Code:
    <html>
    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> 
    </head>
    
    <body>
    <input type="button" id="start" value="show form"/> 
    <script type="text/javascript">
    $(document).ready(function () {
    
        // these bits get done by the page - I have no control at this point
    
        $("#send").live("click", function () {
            $('#theform').remove();
    		alert("form sent!");
        });
    
        $("#start").click(function () {
            $('body').append('<div id="theform"><input type="checkbox" id="cb"/>Check me or you\'ll get an alert<br>' +
                '<input type="button" id="send" value="send form"/></div>')
        });
    
        // this is where the greasemonkeying starts
    
        $("#send").live("mousedown", function () {
            if (!($("#cb").is(":checked"))) {
                    if (confirm("You didn't check the box! Sure you want to continue?")) {
                    $("#send").click();
                }
            }
        });
    
    });
    
    </script>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    if (confirm("You didn't check the box! Sure you want to continue?")) {
    The confirm returns true or false, so this could be written:

    Code:
    if (confirm("You didn't check the box! Sure you want to continue?") == true) {
    Yes, the click event is absorbed by the confirm dialog. [As I recall, it wasn't always; I believe some browsers used to propagate it - that is, pass it on.]
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by xelawho View Post
    And that's the second question: The way I understand it, the event order is mousedown, click, mouseup. So what happens to the click event? Does it die while the confirm box is showing?
    how can a click occur before the mouse button goes up? Note that if you click a button, then drag to ,say the desktop, and release the button, click() is not fired.

    i don't think you can actually terminate/prevent a click() from a mousedown...


    the reason confirm/alert help is that it prevents the mouseup on the element with the click() event, thus ensuring that the button is not actually clicked.



    also , this test app shows this output:

    Code:
    EVT: mousedown
    EVT: mouseup
    EVT: click




    you may want to stop pulling your hair; i found this text on the jQuery site:
    Since the .live() method handles events once they have propagated to the top of the document, it is not possible to stop propagation of live events. Similarly, events handled by .delegate() will propagate to the elements to which they are delegated; event handlers bound on any elements below it in the DOM tree will already have been executed by the time the delegated event handler is called. These handlers, therefore, may prevent the delegated handler from triggering by calling event.stopPropagation() or returning false.
    Last edited by rnd me; 01-13-2013 at 01:09 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Quote Originally Posted by rnd me View Post
    this test app shows this output:
    Code:
    EVT: mousedown
    EVT: mouseup
    EVT: click
    Interesting. I figured it was otherwise for some reason. I should really look some of this stuff up. But as long as nobody uses mousedown, my reign of greasemonkeying terror can continue.

    So, nobody use mousedown, please

    There was no hair-pulling going on. That it works is good enough for me. That it should work on non-windows OSs is even better. Thanks again for the info


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •