SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru mdumka's Avatar
    Join Date
    Jul 2003
    Location
    True North
    Posts
    642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to Set onClick Value

    Hello ...

    I would like to add a function to some of my hyperlinks dynamically when the page loads ... I have to use onClick but I have no idea how to set it.

    The little test I made should call a function to set the onClick of a link to display an alert box ... any help would be great.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script language="javascript">
    function setOnClick() {
    	document.toClick.onClick = "alert('hello');";
    }
    </script>
    </head>
    
    <body>
    <p><a href="javascript: setOnClick();">Set On Click</a></p>
    <p><a href="#" name="toClick">Link To Click</a></p>
    </body>
    </html>
    Thanks

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Untitled Document</title>
            <link rel="stylesheet" type="text/css" media="screen" />
            <style type="text/css"></style>
            <script type="text/javascript">
                function setOnClick() {
                    document.getElementById('toClick').onclick = function() {
                        alert('hello');
                    };
                }
            </script>
        </head>
        <body>
            
            <p><a href="javascript: setOnClick();">Set On Click</a></p>
            <p><a href="#" id="toClick">Link To Click</a></p>
    
        </body>
    </html>



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
  •