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 10 of 10
  1. #1
    New Coder
    Join Date
    Sep 2010
    Location
    High Wycombe, UK
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Variable button name.

    I wanna create a button whose name changes. As it changes it does different things. Here is my best effort:

    <html>
    <body>
    <script type="text/javascript">
    var status == "Ready"; //all spells start out as Ready.
    function butClick() //depending on the status this will do different things.
    {
    if status == 'Ready'
    { call the Cast_Spell function and set Status == 'Not Ready';
    }
    if status == 'Casting...'
    { Alert the user, cannot do anything;
    }
    if status == 'Learning...'
    { Alert the user, cannot do anything;
    }
    else //status == 'Not Ready'
    { set Status == 'Learning...';
    }
    }
    </script>
    <button type="button" onclick="butClick()">
    <script>
    document.write(status)
    </script
    </button>
    </body>
    </html>

    Which of course does not work. Why? Can this be done? I am being lead to believe "Anything can be done!". How, please?

    Thanks,
    Morlaf

  • #2
    New Coder
    Join Date
    Sep 2010
    Location
    High Wycombe, UK
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Perhaps I should have been more specific: Obviously the functions do nothing but I wanna pass the value of the variable status "Ready" to the button name. If I can do that the world is my oyster.....?

  • #3
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    You mean something like this? http://jsfiddle.net/RKgmt/

    However, your transition logic is not complete. Right now, you can't get out of the status "Casting....". Obviously you need to add more logic.

    Please also note that alert() is obsolete and should only be used for debugging purposes. Use DOM methods instead to interact with the user.

  • Users who have thanked Airblader for this post:

    Morlaf (04-12-2013)

  • #4
    New Coder
    Join Date
    Sep 2010
    Location
    High Wycombe, UK
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Airblader View Post
    You mean something like this? http://jsfiddle.net/RKgmt/

    However, your transition logic is not complete. Right now, you can't get out of the status "Casting....". Obviously you need to add more logic.

    Please also note that alert() is obsolete and should only be used for debugging purposes. Use DOM methods instead to interact with the user.
    Thanks for introducing me to "jsfiddle"!!! whoever created that site need some sort of accolade!
    and yes.... that is exactly what i want!
    Thanks,
    Morlaf

  • #5
    New Coder
    Join Date
    Sep 2010
    Location
    High Wycombe, UK
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Why have you got so many variables? Will the following not do the same?

    <html>
    <body>
    <input type="button" id="btn" />
    <script type="text/javascript">
    var button = document.getElementById('btn');
    function castSpell() {
    button.value = 'Casting...';
    };

    button.value = 'Ready';
    button.onclick = function () {
    var currentStatus = this.value;

    switch( currentStatus ) {
    case 'Ready':
    this.value = 'Not ready';
    castSpell();
    break;
    case 'Casting...':
    alert( 'You are already learning!' );
    break;
    case 'Learning...':
    alert( 'You are already learning!' );
    break;
    case 'Not ready':
    this.value = 'Learning...';
    break;
    default:
    alert( 'Error!' );
    break;
    }
    };
    </script>

    </body>
    </html>

    why did you have: var castSpell = function () ???


    just struggling with all the variables.
    Thanks

  • #6
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Your code uses exacttly one variable less – does that really make the difference between "many" and "not many"?
    In any case, actually it's the same numbers of variables. You're just declaring the function differently, which is completely fine. Both ways are correct.

  • #7
    New Coder
    Join Date
    Sep 2010
    Location
    High Wycombe, UK
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts
    a valid point......
    still fiddling about with it as i have decided to reduce the statuses to only two. and use an if loop.
    will update with my final results soon - look forward to your critique.

    Thanks agains,
    Morlaf

  • #8
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by Morlaf View Post
    if loop.
    There is no such thing. The if statement is not a loop.

  • #9
    New Coder
    Join Date
    Sep 2010
    Location
    High Wycombe, UK
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Attempting to accomplish the same with and if statement
    only when the spell is Ready or Not Ready will the player be able to do anything so I think it will be much simpler to have an if-else in there.
    This is the best I managed:<html>
    <body>
    <input type="button" id="btn" />
    <script type="text/javascript">
    var button = document.getElementById('btn');

    function castSpell() {
    button.value = 'Casting...';
    };
    function learnSpell() {
    button.value = 'Learning...'
    };

    button.value = 'Ready';
    button.onclick = function () {
    var currentStatus = this.value;
    }
    if(currentStatus == 'Ready'){
    castSpell();
    // change the button to 'Casting...' for 5" (player cannot interact with this)
    // then change it to 'Not Ready'
    }
    else {
    learnSpell();
    // change button to 'Learning...' for 10" (player cannot interact with this)
    //then chenge it to 'Ready'
    }
    </script>

    </body>
    </html>


    Which does not work - but I cannot figure out why. Am I not actually calling the function when I click the button? why not? Any help appreciated.

    Thanks,
    Morlaf

  • #10
    New Coder
    Join Date
    Sep 2010
    Location
    High Wycombe, UK
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I have managed to fix it!
    i closed the function in the wrong place.
    now i have:
    <html>
    <body>
    <input type="button" id="btn" />
    <script type="text/javascript">
    var button = document.getElementById('btn');

    function castSpell() {
    button.value = 'Not Ready';
    };
    function learnSpell() {
    button.value = 'Ready'
    };

    button.value = 'Ready';
    button.onclick = function () {
    var currentStatus = this.value;


    if(currentStatus == 'Ready'){
    castSpell();
    // change the button to 'Casting...' for 5" (player cannot interact with this)
    // then change it to 'Not Ready'
    }
    else {
    learnSpell();
    // change button to 'Learning...' for 10" (player cannot interact with this)
    //then chenge it to 'Ready'
    }
    }
    </script>

    </body>
    </html>


    Which does what i want.
    Thanks!

    Morlaf (will be replying to himself some more soon!)


  •  

    Posting Permissions

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