1

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>    

4 Answers 4

4

Not 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 becomes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your response. It's very much appreciated and works like a charm!
1
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 comparing, so use == instead of =

1 Comment

Thank you for having responded. It's very much appreciated!
1

use this for your problem it will help you better-

  1. check equal use '=='
  2. 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>
    

1 Comment

Thank you for having responded. It's very much appreciated!
0

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>

1 Comment

Thank you for having responded. It's very much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.