SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Implementing a confirm box

    Hello

    I'm working on a new project and I want to have all my javascript outside the html source.

    I want to add a confirm box on all links having the class "removelink", I use Prototype as framework.

    Main problem: I can't simulate onclick="return confirm('...')" in a proper way without disable the click when the user cancels.

    This is what I have now:

    HTML:
    <a href="delete.php?id=1" class="removelink">Test</a>

    javascript:
    Code JavaScript:
     $$('.removelink').each(function(item){
    item.observe('click', function(){
    if(confirm("Are you sure you want to delete?")){ return true; }
    else{ return false; }
    });
    });

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'm not familiar with Prototype, but when you are putting JS outside HTML, then you need to take a slightly different route when you want to cancel the click's action (i.e. go to the location the link points to).
    Code Javascript:
    $$('.removelink').each(function(item){
      item.observe('click', function(e) {
        if(!confirm("Are you sure you want to delete?")) {
          if (!e) window.event.returnValue = false;
          else e.preventDefault();
        }
      }
    }

    Edit:

    Of course, there is a "Prototype way" where all you need to do is Event.stop() or I suppose in your case item.stop()

  3. #3
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you!

    This solved my problem:
    Code JavaScript:
    if(!confirm("Are you sure...")){
    e.stop();
    }


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
  •