I'm trying to have a javascript function in my HTML page that shows more text whenever you click on a name, and then hides it if you click on the same name again or -eventually- shows different text when you click on another name. This is my HTML code:
<section>
<p id="one" onclick="myFunctionOne()">Cheap table X800</p>
<p id="two" onclick="myFunctionTwo()">Luxury table Z2100</p>
<p id="three" onclick="myFunctionThree()">Chair 101</p>
</section>
And this is the Javascript:
<script>
var oneS = "<ul>"+"<li>ID: 200</li>"+"<li>Name: Cheap table X800</li>"+"<li>Description: This is the cheapest table you can find that still meets its goal. 4 people max.</li>"+"</ul>";
var twoS = "<ul>"+"<li>ID: 201</li>"+"<li>Name: Luxury table Z2100</li>"+"<li>Description: A long table, suitable for a meeting room.</li>"+"</ul>";
var threeS = "<ul>"+"<li>ID: 202</li>"+"<li>Name: Chair 101</li>"+"<li>Description: A very basic but functional chair.</li>"+"</ul>";
var booleanOne=false;
var booleanTwo=false;
var booleanThree=false;
function myFunctionOne() {
//alert("outside if "+booleanOne);
if (!booleanOne){
alert("inside if "+booleanOne);
document.getElementById("one").innerHTML += oneS;
booleanOne=true;
alert(booleanOne);
if (booleanTwo) myFunctionTwo();
if (booleanThree) myFunctionThree();
booleanOne=true;
return;
}
//alert("outside else "+booleanOne);
else if (booleanOne) {
alert("inside else "+booleanOne);
document.getElementById("one").innerHTML = "<p id="+"\"one\""+" onclick="+"\"myFunctionOne()\""+">Cheap table X800</p>";
booleanOne=false;
alert(booleanOne);
return;
}
}
function myFunctionTwo() {
if (!booleanTwo){
document.getElementById("two").innerHTML += twoS;
booleanTwo=true;
if (booleanThree) myFunctionThree();
if (booleanOne) myFunctionOne();
}
else {
document.getElementById("two").innerHTML = "<p id="+"\"two\""+" onclick="+"\"myFunctionTwo()\""+">Luxury table Z2100</p>";
booleanTwo=false;
}
}
function myFunctionThree() {
if (!booleanThree){
document.getElementById("three").innerHTML += threeS;
booleanThree=true;
if (booleanOne) myFunctionOne();
if (booleanTwo) myFunctionTwo();
}
else{
document.getElementById("three").innerHTML = "<p id="+"\"three\""+" onclick="+"\"myFunctionThree()\""+">Chair 101</p>";
booleanThree=false;
}
}
</script>
I know this may not be the best way of doing things, but it's the first time I try to use Javascript/HTML. What I've been trying to do for testing purposes is to click on Cheap table X800 for three times in a row, and following the logic of the program what it should do is show the product description on the first click, close it on the second and then open it again on the third one. When I try to do it, though, the first two clicks work as expected but the third one - from what I get thanks to the alerts - calls myFunctionOne, goes inside the if statement, executes everything, goes inside the else statement and executes that bit of code too. This is only visible with the alerts stalling the script on the page, otherwise the third click seems to do nothing. How can I fix this?
document.getElementById("one").innerHTML = "<p id="+"\"one\""+" onclick="+"\"myFunctionOne()\""+">Cheap table X800</p>";.it will create anotherpelement insidepwithid=one.instead just usedocument.getElementById("one").innerHTML = "Cheap table X800;