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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    4
    Thanked 0 Times in 0 Posts

    An onclick problem

    i want to by pressing an button to result an hint out 5 known and i cant it done right.Can you help me with this?(the reak task is to spawn them randomly but i couldnt imagine how that could be--so if have ideas please tell me) Thanks in anticipation

    <!DOCTYPE html>
    <html>
    <body>

    <p>Click the button to display a hint to help you study.</p>

    <button onclick="myFunction()"&&"count()">Try it</button>

    <p id="demo"></p>

    <script type="text/javascript">
    var clicks=0;
    function count()
    {

    clicks++;

    }
    var x;
    function myFunction()
    {

    switch(clicks)
    {
    case 1:
    x="Atend to your lectures and labs all the time and take notes.";
    case 2:
    x="Try planning yor time so that you have a certain amount of hours to study.";
    case 3:
    x="As soon as you get a new lecture try to learn it so that work doesn't stack up.";
    case 4:
    x="Try different sources of information,don't use just one.";
    case 5:
    x="Don't let your attention be diverted when you study.";
    default:
    x="no more tips";

    }
    document.getElementById("demo").innerHTML=x;
    }
    </script>
    </body>
    </html>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    You forgot the 'break' after each case in the switch statement.
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <p>Click the button to display a hint to help you study.</p>
    <button onclick="myFunction()">Try it</button>
    <p id="demo"></p>
    
    <script type="text/javascript">
    var clicks=0;
    function myFunction() {
      clicks++;
      var x = '';
      switch(clicks) {
        case 1:  x="Atend to your lectures and labs all the time and take notes."; break;
        case 2:  x="Try planning yor time so that you have a certain amount of hours to study."; break;
        case 3:  x="As soon as you get a new lecture try to learn it so that work doesn't stack up."; break;
        case 4:  x="Try different sources of information,don't use just one."; break;
        case 5:  x="Don't let your attention be diverted when you study."; break;
        default: x="no more tips"; break;
      }
      document.getElementById("demo").innerHTML=x;
    }
    </script>
    </body>
    </html>
    BTW, you should use [ code] and [ /code] tags without the spaces
    to make it easier for forum readers to view, copy, test and/or debug your scripts.

  • Users who have thanked jmrker for this post:

    valyii (11-05-2012)

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by valyii View Post
    the real task is to spawn them randomly
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click the button to display a hint to help you study.</p>
    
    <button id='btn'>Try it</button>
    
    <p id="demo"></p>
    
    <script type="text/javascript">
    
    function myFunction()
    {
      var hints = [ "Atend to your lectures and labs all the time and take notes.",  
                    "Try planning yor time so that you have a certain amount of hours to study.",
                    "As soon as you get a new lecture try to learn it so that work doesn't stack up.",
                    "Try different sources of information,don't use just one.",
                    "Don't let your attention be diverted when you study.",
                    "Learn to spell"
                  ],
         last = -1;
    
      return function()
      {    
        while( ( temp = Math.floor( Math.random() * hints.length ) ) == last )
        ;  
       
        document.getElementById("demo").innerHTML = hints[ last = temp ];
      }
    }
    
    document.getElementById( 'btn' ).onclick = myFunction();
    
    </script>
    </body>
    </html>

  • Users who have thanked Logic Ali for this post:

    valyii (11-05-2012)

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by jmrker View Post
    BTW, you should use [ code] and [ /code] tags without the spaces
    to make it easier for forum readers to view, copy, test and/or debug your scripts.
    Not any more it doesn't! Since the new owners took over (1st November) if you copy text within code tags the line breaks are lost and a message is appended

    Read more at An onclick problem

    I do hope this will be fixed quickly!!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Philip M View Post
    Read more at An onclick problem
    That's a circular reference; are you trying to cause a memory leak?

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Exclamation

    Quote Originally Posted by Philip M View Post
    Not any more it doesn't! Since the new owners took over (1st November) if you copy text within code tags the line breaks are lost and a message is appended

    Read more at An onclick problem

    I do hope this will be fixed quickly!!
    That's a bummer!
    I had not read about the change.

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Quote Originally Posted by Philip M View Post
    Since the new owners took over (1st November) if you copy text within code tags the line breaks are lost and a message is appended
    annoying, huh?

    in the meantime, I note that if you hit "Reply to post" and then copy the code from within the posts that appear below, it retains the line breaks.

    still, curious that you would take something that was previously working fine and fudge it up for no apparent gain...

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by xelawho View Post
    annoying, huh?

    in the meantime, I note that if you hit "Reply to post" and then copy the code from within the posts that appear below, it retains the line breaks.

    still, curious that you would take something that was previously working fine and fudge it up for no apparent gain...
    Thanks for showing the way around! Relief!

    I think it is something to do with Facebook and Twitter which seem to have icons appearing.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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