SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    blonde.... Sarah's Avatar
    Join Date
    Jul 2001
    Location
    Berkshire, UK
    Posts
    7,442
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Can't see for looking! OnClick for hyperlink???

    I probably should come back to this in the morning as I think my brain is fried..

    Anyway I am trying to add in some functionality to my ASP scripts, I currently have a hyperlink
    Code:
    <a href="delete_record.asp">delete</a>
    and when clicked the record is deleted - I have now been asked to provide an alert pop up box saying "That's the delete button - Are you Sure?" BUT I can only find OnClick details for Form buttons - but surely I can do the same for a link?

    I know that I could use OnMOuseOver/Out etc but I only want to button to pop up if they click the link, and the options of Yes/No/Cancel and if Yes the script completes and the record is deleted?

    So how daft am I being?

    Thanks
    Sarah
    Regular user

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could do it like this...

    Code:
    <a href="delete_record.asp" onclick="if(confirm('Are you sure?')){return true}else{return false}">delete</a>
    travis

  3. #3
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the following, clicking the link will cause the page "delete_record.asp" to be loaded only if delRec() returns true or if javascript is disabled.
    Code:
    <html>
    <head>
    <script type='text/javascript'>
    function delRec() {
      if (confirm("Are you sure you want to delete the record?")) return true;
      else return false;
    }
    </script>
    </head>
    <body>
    <p><a href="delete_record.asp" onclick="return delRec()">Delete Record</a></p>
    </body>
    </html>

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    podlob and I posted the same answer at the same time

  5. #5
    blonde.... Sarah's Avatar
    Join Date
    Jul 2001
    Location
    Berkshire, UK
    Posts
    7,442
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Cheers Guys, so how come my js book said that I couldn't use OnClick for hyperlinks??

    Anyway thanks I will get that sorted first thing in the morning

    Sarah
    Regular user

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What book are you reading? You can absolutely put an onclick event in an anchor tag.

    While both my example and MikeFoster's achieve the same result, I would suggest using MikeFoster's if you're going to have more than 1 instance of the delete element on the page.
    travis

  7. #7
    SitePoint Member dotjeff's Avatar
    Join Date
    Dec 2002
    Location
    Oregon
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Some more food for thought

    Sarah,

    Here's something else to consider in your implementation. This is a perfect case where the "return" statement would be perfect. I wrote an article awhile ago on how it works and it uses a link with an onclick event handler as one of the examples.

    JavaScript: The Point of No Return?!

    Also, you'd probably find the information on MSDN useful about events, which objects they apply to, and whether or not they're cancellable.

    http://msdn.microsoft.com/workshop/a...nce/events.asp

    Good luck,

    Jeff (.jeff)

  8. #8
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Sarah
    Cheers Guys, so how come my js book said that I couldn't use OnClick for hyperlinks??

    Anyway thanks I will get that sorted first thing in the morning

    Sarah
    Your JS book seems to be dirt:
    http://www.w3schools.com/tags/tag_a.asp

    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  9. #9
    blonde.... Sarah's Avatar
    Join Date
    Jul 2001
    Location
    Berkshire, UK
    Posts
    7,442
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks Guys, PobLob I used your example as it fitted in better with my page structure - and thanks it worked perfectly. Although I will have many instance on one page you can only click one at a time.

    The book is just a simple Js book that up until now has been dead easy to use and follow - I think its time I upgraded.

    Jeff, following up those links now.

    Thanking you

    Sarah
    Regular user

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I also followed up those links, and it appears that my original code was not optimised as much as it could have been. Since confirm() returns a true/false value there's no reason to use an if/else statment.


    Code:
    <a href="delete.asp" onClick="return confirm('Are you sure?')">Delete</a>
    Last edited by podlob; Dec 14, 2002 at 14:11.
    travis


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
  •