Nima Amin
Nima Amin

Reputation: 92

JS Change text onClick then change back onClick If...else

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>    

Upvotes: 1

Views: 6384

Answers (4)

Florian Margaine
Florian Margaine

Reputation: 60717

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.

Upvotes: 4

Manish Nagar
Manish Nagar

Reputation: 1046

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>
    

Upvotes: 1

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35963

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>

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

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 =

Upvotes: 1

Related Questions