Take an action when Clicking a link

Discussion in 'JavaScript' started by samuvk, Jul 15, 2013.

  1. #1
    How can a do to execute a function when clicking a link?

    Something like:


    <p>Link</p>

    <script>
    $("p").click(function () {
    $(this).myFunction();
    });
    </script>


    <script> function myFunction(
    )
    </script>
     
    samuvk, Jul 15, 2013 IP
  2. scriptjerk

    scriptjerk Active Member

    Messages:
    43
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    58
    #2
    <script>
    $("p").click(function () {
      myFunction();
    });
    </script>
    Code (markup):
    Like that
     
    scriptjerk, Jul 15, 2013 IP
  3. GuiltyCrown

    GuiltyCrown Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    <script>
    $("#link_id").click(function () {

    alert("Clicked");
    return false;
    });
    </script>
    This will disable the normal behaviour of the link so if the href is # it will not get you to the top of the page, it won't do anything else than showing the alert box. Even if the href is google.com it won't redirect there.
     
    GuiltyCrown, Jul 16, 2013 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    $(document).ready(function(e) {
    $(document).delegate("#link_id","click",function(){
    if(confirm("Would you like to continue to the link ?")) {
    return true;
    } else {
    //in case you don't want to disable the behaviour of the link
    e.stopPropagation();
    e.preventDefault();
    return false;
    }
    });
    Code (markup):
    Try the above...
     
    tvoodoo, Jul 17, 2013 IP