Here's an easy one.
I'm creating a link with content that changes onClick. Onload it should say ""Click here for more information!" when clicked, it should say "Click here for less information!" then on re-click "...more information".
I'm sure that I'm just making a tiny mistake somewhere, help?
JavaScript
<script>
function change_text()
{
if(document.getElementById("toggle_button").innerHTML="Click here for more
information!"){
document.getElementById("toggle_button").innerHTML="Click here for less
information!";
}else{
document.getElementById("toggle_button").innerHTML="Click here for more
information!";
}}
</script>
Here's the HTML
<a href='javascript: toggle()'><p id="toggle_button" onclick="change_text()">Click here
for more information!</p></a>
Here's an easy one.
I'm creating a link with content that changes onClick. Onload it should say ""Click here for more information!" when clicked, it should say "Click here for less information!" then on re-click "...more information".
I'm sure that I'm just making a tiny mistake somewhere, help?
JavaScript
<script>
function change_text()
{
if(document.getElementById("toggle_button").innerHTML="Click here for more
information!"){
document.getElementById("toggle_button").innerHTML="Click here for less
information!";
}else{
document.getElementById("toggle_button").innerHTML="Click here for more
information!";
}}
</script>
Here's the HTML
<a href='javascript: toggle()'><p id="toggle_button" onclick="change_text()">Click here
for more information!</p></a>
Share
Improve this question
asked Dec 20, 2012 at 8:19
Nima AminNima Amin
922 silver badges11 bronze badges
4 Answers
Reset to default 4Not only are you misusing =
as ===
, but you can also greatly improve your code with a simple technique: caching.
function change_text() {
var button = document.getElementById('toggle_button');
if (button.innerHTML === "Click here for more information!") {
button.innerHTML = "Click here for less information!";
}
else {
button.innerHTML = "Click here for more information!";
}
}
You can see how way clearer the code bees.
if(document.getElementById("toggle_button").innerHTML="Click here for more
information!"){
should be
if(document.getElementById("toggle_button").innerHTML == "Click here for more
information!"){
you are assigning rather than paring, so use ==
instead of =
use this for your problem it will help you better-
- check equal use '=='
javascript:void(0) use on href
<html> <head> <title>index</title> </head> <body> <script type="text/javascript"> function change_text() { if(document.getElementById("toggle_button").innerHTML=="Click here for more information!") { document.getElementById("toggle_button").innerHTML="Click here for less information!"; } else { document.getElementById("toggle_button").innerHTML="Click here for more information!"; } } </script> <a href='javascript:void(0)' onclick="change_text()"><p id="toggle_button" >Click here for more information!</p></a> </body> </html>
You have miss "=" in
if(document.getElementById("toggle_button").innerHTML="Click here for more
information!")
Try this code:
<script>
function change_text()
{
if(document.getElementById("toggle_button").innerHTML=="Click here for more
information!"){
document.getElementById("toggle_button").innerHTML="Click here for less
information!";
}else{
document.getElementById("toggle_button").innerHTML="Click here for more
information!";
}}
</script>