Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    problem with accessing onclick attribute

    What I am trying to achieve, is a button that calls a function that changes the onclick attribute of that button, so that when it is clicked again, it resets the onclick attribute to call the default function. here is what the stripped down code looks like:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function remove_prep(a){
    a.value="cancel";
    a.onclick="cancel_remove(this);";
    }
    function cancel_remove(a){
    a.value="remove";
    a.onclick="remove_prep(this);";
    }
    </script>
    </head>
    <body>
    <input type="button" value="remove" onclick="remove_prep(this);" />
    </body>
    </html>
    for some reason, it only fires once, after which it ceases to work properly.


    thanks for the help this part of my code is killing me.
    Last edited by BirdIsTheWord; 02-08-2012 at 04:29 AM.

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,910
    Thanks
    56
    Thanked 544 Times in 541 Posts
    you want it to be able to flip back and forth infinitely?
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var clicked=false;
    function duck(){
    alert("wabbit season")
    clicked=true;
    }
    function wabbit(){
    alert("duck season")
    clicked=false;
    }
    </script>
    </head>
    <body>
    <input type="button" value="click me!" onclick="clicked==false?duck():wabbit()" />
    </body>
    </html>

  • Users who have thanked xelawho for this post:

    BirdIsTheWord (02-08-2012)

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    yes, this is great, thank you!

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,910
    Thanks
    56
    Thanked 544 Times in 541 Posts
    you're welcome. there's actually lots of ways to do this. here's another which is a bit closer to what you were trying:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function duck(btn){
    alert("wabbit season")
    btn.onclick=function(){wabbit(btn)}
    }
    function wabbit(btn){
    alert("duck season")
    btn.onclick=function(){duck(btn)}
    }
    </script>
    </head>
    <body>
    <input type="button" value="click me!" onclick="duck(this)" />
    </body>
    </html>

  • #5
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thank you, seriously I've been puzzled over why it didn't work for the past two days, now I can focus on other things, like sleep

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    just remember that the HTML event attributes are not the same as the JavaScript event properties. the former require a parseable JavaScript expression/statement, while the latter require a function. there are also differences in the used scope and automatically passed parameters.

    PHP Code:
    function test()
    {
        
    alert(this); // show scope
        
    alert(arguments[0]); // show 1st passed parameter

    PHP Code:
    <button type="button" id="a_button" onclick="test();">click me</button>
    <
    button type="button" id="another_button">click me</button>
    <
    script type="text/javascript">
    document.getElementById("another_button").onclick test;
    </script> 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •