...

View Full Version : An onclick problem



valyii
11-05-2012, 03:24 AM
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>

jmrker
11-05-2012, 04:55 AM
You forgot the 'break' after each case in the switch statement.


<!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.

Logic Ali
11-05-2012, 05:53 AM
the real task is to spawn them randomly

<!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>

Philip M
11-05-2012, 08:25 AM
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 http://www.codingforums.com/showthread.php?t=280894#BRaxZVMt7bDxKrVI.99

I do hope this will be fixed quickly!!

Logic Ali
11-05-2012, 02:42 PM
Read more at http://www.codingforums.com/showthread.php?t=280894#BRaxZVMt7bDxKrVI.99


That's a circular reference; are you trying to cause a memory leak?

jmrker
11-05-2012, 04:56 PM
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 http://www.codingforums.com/showthread.php?t=280894#BRaxZVMt7bDxKrVI.99

I do hope this will be fixed quickly!!

That's a bummer! :eek:
I had not read about the change. :confused:

xelawho
11-05-2012, 05:57 PM
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...

Philip M
11-05-2012, 06:12 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum