What's new
HTML Forums | An HTML and CSS Coding Community

Welcome to HTMLForums; home of web development discussion! Please sign in or register your free account to get involved. Once registered you will be able to connect with other members, send and receive private messages, reply to topics and create your very own. Our registration process is hassle-free and takes no time at all!

on click button not working. Could you please help me in identifying the bug

Kiriti

New member
<!DOCTYPE html>
<html>
<head>
<title>program 1a</title>
</head>
<body style="background-color:green">
<h2>when you click on press me it will change text</h2>
<p id="Demo">Gokaraju</p>
<button onclick="Change text()">click me
</button>
<script>
function "Change text()"
{
if(document.getElementById("Demo").innerHTML="Gokaraju");
{
document.getElementById("Demo").innerHTML="2nd CSE A");
}
else
{
document.getElementById("Demo").innerHTML="Siva";
}
}
</script>
</body>
</html>
 
Change text() is a method. Syntax is wrong. Use or name it properly. myFunction() or changeText() which ever suits you. Into your coding.

<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World"; //or something else. Add your own words. It is not a conditional statement.
}
</script>
What I mean?
 
What you wanted was 3 choices. Gokaraju, 2nd CSE A, and Siva when pressed three times. Then you need to write it differently. Value returns to the original text or null because a space in memory is being used. The next option. 2nd CSE A. Now it's conditional. Try this in your script.

Code:
// declaring a value
//like that

let i = 0;
let text = " "; //add the names here.  2nd name.  ,  ... and so on.
for (; i < text; i++) { 
  text += text;
} //end for loop

Bottom is another writing.  You can write it any variations.

x[1]=" "
x[2]=" "
x[3]=" "

for (let i = 0; i < x;  i++) { 
   i;
} //end loop

So for 4 values of i. 1 for null. So the program repeats itself. Pressing it three times.
 
Last edited:
Here is the final code
Code:
<body>
<p id="Demo">
<button onclick="changeText()"> click me </button>
<script>
function changeText();
let i=0;
let text=" Gokaraju" , " 2nd CSE A ", " Siva";
for( i; i < text ; i++)  {
text += text;
} //end loop
document.getElementByid("Demo").innerHTML(text);
</script>
</p>
</body>

Press it 3 times for all names

output is:

Gokaraju
2nd CSE A
Siva
 
Last edited:
Back
Top