I am making a dislike/like system where it displays when you liked or disliked something on the screen after you clicked on the image input.
My problem is, for example:
When I click on thumbs up a LIKED
message appears like it should, but then I click the thumbs downs and the DISLIKED
appears under the LIKED
.
I would like it to be that if I click on the thumbs down then the LIKED
message disappears and the DISLIKED
appears.
function ai() { document.getElementById("liked").innerHTML = "LIKED"; } function aii() { document.getElementById("disliked").innerHTML = "DISLIKED"; }
<h1 id="liked"></h1> <h1 id="disliked"></h1> <input type="image" src="tu.png" class="tu" onclick="ai()"> <input type="image" src="td.png" class="td" onclick="aii()">
You can create only 1 element and do it like following
function ai() { document.getElementById("message").innerHTML = "LIKED"; } function aii() { document.getElementById("message").innerHTML = "DISLIKED"; }
<h1 id="message"></h1> <input type="image" src="tu.png" class="tu" onclick="ai()"> <input type="image" src="td.png" class="td" onclick="aii()">
Or you can improve your code like below
function ai() { document.getElementById("liked").innerHTML = "LIKED"; document.getElementById("disliked").innerHTML = ""; } function aii() { document.getElementById("disliked").innerHTML = "DISLIKED"; document.getElementById("liked").innerHTML = ""; }
<h1 id="liked"></h1> <h1 id="disliked"></h1> <input type="image" src="tu.png" class="tu" onclick="ai()"> <input type="image" src="td.png" class="td" onclick="aii()">
Show and hide instead - Do be aware that type="image" is a submit button if inside a form:
Version 1: only one H1
function ai(img) { console.log(img.className) document.getElementById("liked").innerHTML = img.className == "tu" ? "LIKED" : "DISLIKED"; document.getElementById("liked").style.display = "block"; }
.like { display: none }
<h1 class="like" id="liked"></h1> <img src="tu.png" class="tu" onclick="ai(this)" /> <img src="tu.png" class="td" onclick="ai(this)" />
Version 2 - both on page:
function ai(img) { console.log(img.className) document.getElementById(img.className == "tu" ? "liked" : "disliked").style.display = "block"; document.getElementById(img.className == "tu" ? "disliked" : "liked").style.display = "none"; }
.like { display: none }
<h1 class="like" id="liked">LIKED</h1> <h1 class="like" id="disliked">DISLIKED</h1> <img src="tu.png" class="tu" onclick="ai(this)" /> <img src="tu.png" class="td" onclick="ai(this)" />
function ai(temp) { if (temp == "1") { document.getElementById("Result").innerHTML = "LIKED"; } else { document.getElementById("Result").innerHTML = "DISLIKED"; } }
<h1 id="Result"></h1> <input type="image" src="tu.png" class="tu" onclick="ai(1)"> <input type="image" src="td.png" class="td" onclick="ai(0)">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.