SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cancel button onclick event Firefox

    Hi everyone,

    I know you can cancel a Firefox onclick event on a button by doing this

    <input type='button' onclick = 'return Validate()' />

    where Validate returns false.

    However is is possible to cancel the event from within the Validate method without having to have the 'return' in the attribute i.e.

    <input type='button' onclick = 'Validate()' />

    I have messed around with event.stopPropogation and event.cancelBubble=true, event.returnValue=false and nothing works.

    Can anyone help me - is this possible?

    cheers,
    Robert

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's an idea that sort of does what you asked - however you'd need to register the events to objects separately:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function Validate(event) {
    	var thereturn = confirm("Hit OK to return true, Cancel to return false");
    	if (event) {
    		// mozilla
    		if (!thereturn) {
    			event.preventDefault();
    		}
    	} else {
    		return thereturn;
    	}
    }
    
    window.onload = function () {
    	document.getElementById("thesubmit").onclick = Validate;
    }
    </script>
    </head>
    <body>
    <form method="post" action="nextpage.php">
    <input id="thesubmit" type='submit' />
    </form>
    </body>
    </html>

  3. #3
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot Jim - that worked a treat.

    Do you know a good source for finding out things such as 'preventDefault()'?

  4. #4
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bertnz View Post
    Do you know a good source for finding out things such as 'preventDefault()'?
    http://developer.mozilla.org/

    For example, here are details on preventDefault.


Bookmarks

Posting Permissions

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