Change link text in HTML using JavaScript

I have an html page that has a link called "open". After clicking the link, the text “open” should change to “close”. How to do it?

+3
source share
4 answers

Script

<html>
  <head>
    <script type="text/javascript">
      function open_fun() {
        document.getElementById('link').innerHTML = "<a href='javascript:clo_fun()'>CLOSE</a>";
      }
      function clo_fun() {
        document.getElementById('link').innerHTML = "<a href='javascript:open_fun()'>OPEN</a>";
      }
    </script>
  </head>
  <body>
    <div id='link'><a href='javascript:open_fun()'>OPEN</a></div>
  </body>
</html>
+4
source
<a href="" onClick="javascript:this.innerHTML = 'close'">Open</a>

You can also call some switching function to replace the text with a few taps.

function toggle(lnk_obj){
    lnk_obj.innerHTML = (lnk_obj.innerHTML == 'close') ? 'open' : 'close' ;
}


<a href="" onClick="javascript:toggle(this);">Open</a>
+11
source

addEventListener IE. onclick , :

elm.onclick = function (e) {
    this.innerHTML = "close";
};
+3

elm - :

elm.addEventListener("click", function(){
  this.innerHTML = "close";
}, true);
0

Source: https://habr.com/ru/post/1722304/


All Articles