Results 1 to 8 of 8

Thread: Dynamically changing an elements onClick event

  1. #1
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dynamically changing an elements onClick event

    I need to dynamically change the onClick event of an element. I just can't figure out how to.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Change it entirely, or just a part of it?

    If it is a hard coded onclick event, all you need to do is:

    Code:
    el.onclick=function(){whatever;};
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Change it entirely, or just a part of it?

    If it is a hard coded onclick event, all you need to do is:

    Code:
    el.onclick=function(){whatever;};
    Not working...

    document.getElementById(targetHeaderText).onClick = "displayFolder(16)"

    when I snoop it with msgbox immediately after it the msgbox shows displayFolder(16) however when I click the div nothing happens.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    In javascript, onclick is (as I wrote) all lower case:

    Code:
    document.getElementById(targetHeaderText).onclick = "displayFolder(16)"
    Also, if targetHeaderText is the literal id of the element, it needs to be quoted within the document.getElementById() method, and you must assign as a function to pass parameters (also as I wrote):

    Code:
    document.getElementById('targetHeaderText').onclick = function(){displayFolder(16);};
    Try out this demo:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    onload=function(){
    document.getElementById('targetHeaderText').onclick = function(){displayFolder(16);};
    }
    function displayFolder(n){
    alert(n);
    }
    </script>
    </head>
    <body>
    <div id="targetHeaderText">Target Header Text</div>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    In javascript, onclick is (as I wrote) all lower case:

    Code:
    document.getElementById(targetHeaderText).onclick = "displayFolder(16)"
    Also, if targetHeaderText is the literal id of the element, it needs to be quoted within the document.getElementById() method, and you must assign as a function to pass parameters (also as I wrote):

    Code:
    document.getElementById('targetHeaderText').onclick = function(){displayFolder(16);};
    Try out this demo:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    onload=function(){
    document.getElementById('targetHeaderText').onclick = function(){displayFolder(16);};
    }
    function displayFolder(n){
    alert(n);
    }
    </script>
    </head>
    <body>
    <div id="targetHeaderText">Target Header Text</div>
    </body>
    </html>
    I need the example in VBScript as I'm keeping all the code in VBS. The HTML page is for an Outlook Folder Home Page and thus will never be viewed using Opera, Firefox!, or any browser other than a Microsoft produced one.

    targetHeaderText is a variable passed into the function designating the specific <div> that will be updated.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by dch31969 View Post
    I need the example in VBScript as I'm keeping all the code in VBS. The HTML page is for an Outlook Folder . . .
    Then you should have said so in the first place. I'm no expert in VBS. Moving thread to 'Other'.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Then you should have said so in the first place. I'm no expert in VBS. Moving thread to 'Other'.

    Sorry about that, the script is for a Folder Home Page which will replace my Outlook Today - I've been living in the world of VBScript.

  8. #8
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ended up nesting a <span> into the <div> and then using code to update the div's innerHTML property. As in...

    document.getElementById(targetHeaderText).innerHTML = "<span onClick='displayFolder(" & targetFolderConstant & ")'>" & targetFolder & "</span>"

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
  •