0

I need to change text each time I click on button.

var button = document.getElementById("changeText");
button.addEventListener(
"click",
function () {
    if (button.getAttribute("data-text") == button.innerHTML) {
        button.innerHTML = button.getAttribute("data-text1");
    } else {
        button.setAttribute("data-text1", button.innerHTML);
        button.innerHTML = button.getAttribute("data-text");
    }
},
false
);
<div>
<button id="changeText" data-text="Show" data-text1="Hide">Hide</button>
</div>

I don't understand why this code doesn't work when I try to load page using google chrome. However when I loaded it to codepen it worked

3
  • You shouldn't make up your own attributes. If you need custom data, use data-XXX attributes. Commented Apr 30, 2021 at 16:26
  • It works in the snippet here. Commented Apr 30, 2021 at 16:28
  • Are there any errors in the console when you run it on your real web page? Commented Apr 30, 2021 at 16:29

4 Answers 4

2

It expects from you certain structure like this one:

<!DOCTYPE html>  
<html>
    <head>
        <title>
              
        </title>
    </head>
      
    <body>
        <div>
            <button id="changeText" text="Show" >Hide</button>
        </div>

        <script>
            var button = document.getElementById("changeText");
button.addEventListener(
    "click",
    function () {
        if (button.getAttribute("text") == button.innerHTML) {
            button.innerHTML = button.getAttribute("text1");
        } else {
            button.setAttribute("text1", button.innerHTML);
            button.innerHTML = button.getAttribute("text");
        }
    },
    false
);
        </script>
    </body>
</html>

Copy paste it to your file and you'll see that it works. Please mark it as an answer if it fixes your problem :)

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

4 Comments

it works, but i have two separate files .HTML and .js and it doesn't work that way
Try to check correctness of your js path. Are there any other errors?
no other errors, the path is <script defer src="part2.js"></script>
Make sure that your script is located inside body tag, also check if your .js script is in the same folder where your .html file is. I have just separated that piece of code that I posted to 2 different files (.js and .html) in the same folder and it still works.
0

Your code is working, but your approach is not so nice. See the 2 options down below.

let text = {
 'Hide': 'Show',
 'Show': 'Hide'
}

const click = (event) => {
  // option 1, it needs the object above.
  // It's good for multiple alternatiosn like color, icon etc
  // or multiple states like hide to show, show to sure, sure to really, really to hide. 
  event.target.innerText = text[event.target.innerText];
  
  // option 2, it's good for one or two alternations.
  // event.target.innerText = event.target.innerText == 'Hide' ? 'Show' : 'Hide'
}

document.querySelector('button').addEventListener('click', click);
<button>Show</button>

Comments

0

you can make it more simple by removing the custom attribute and use the button innerHTML only along with enum by using Object.freeze(),it will make the code more readable

const titleEnum = Object.freeze({show: "SHOW",  hide: "HIDE"});
var button = document.getElementById("changeText");
button.addEventListener(
    "click",
    function () {
        if (button.innerHTML === titleEnum.hide) {
            button.innerHTML = titleEnum.show;
        } else {
            button.innerHTML = titleEnum.hide;
        }
    },
    false
);
<!DOCTYPE html>  
<html>
    <head>
        <title>
              
        </title>
    </head>
      
    <body>
        <div>
            <button id="changeText">HIDE</button>
        </div>
    </body>
</html>

Comments

0

Use innerText to get the value of an element

const changeBtn = document.getElementById("changeText");
changeBtn.addEventListener("click", ()=>{
    if(changeBtn.innerText === "2"){
        changeBtn.innerText = "1";
    }
    else{
        changeBtn.innerText= "2";
    }
});
<div>
    <button id="changeText">1</button>
</div>

Comments

Your Answer

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