change link text in HTML using JavaScript

I have an html page which has a link called “open”. Once the link is clicked the text “open” should change to “close”. How do I do this?

64 thoughts on “change link text in HTML using JavaScript”

  1. addEventListener is not supported in IE. If you don’t need other onclick events on the link, use this:

    elm.onclick = function (e) {
        this.innerHTML = "close";
    };
    
    Reply
  2. 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>
    
    Reply
  3. Say you have link:

    <a id="link" href="https://www.example.com">open</a>
    
    const link = document.getElementById("link") // HTMLAnchorElement
    const text = link.text; // open
    

    To change value:

    link.text = 'closed'; // now link.text === 'closed'
    

    Since I get downvoted for no reason (this code actually works totally fine) I will add a a fiddle to demonstrate for the non believers that this is solid code and works as expected.

    Also check documentation here on MDN for the text property on an HtmlAnchorElement:

    HTMLAnchorElement.text
    Is a DOMString being a synonym for the Node.textContent property.

    So instead of changing the text node you can set this text property directly.


    Note: Setting innerHTML (advised in other answers) is not considered safe since it can be used for injecting malicious code into the DOM.

    Reply
  4. <a href="" onClick="this.innerHTML = 'close'">Open</a>
    

    You could also call some type of toggle function for swapping the text on multiple clicks.

    function toggle(lnk_obj){
        lnk_obj.innerHTML = (lnk_obj.innerHTML == 'close') ? 'open' : 'close' ;
    }
    
    
    <a href="" onClick="javascript:toggle(this);">Open</a>
    
    Reply
  5. I am extremely impressed with your writing skills and also with the layout on your weblog.

    Is this a paid theme or did you modify it yourself?
    Either way keep up the nice quality writing, it is rare to see
    a great blog like this one these days.

    Reply
  6. Excellent blog you have here but I was wanting to know if you knew of any forums that cover the same topics talked about in this article?
    I’d really love to be a part of online community where I can get responses from other experienced people that share the same interest.
    If you have any recommendations, please let me know.
    Appreciate it!

    Review my homepage: pussy888 android

    Reply
  7. Great blog! Do you have any tips and hints for aspiring writers?
    I’m hoping to start my own website soon but I’m a little lost on everything.
    Would you propose starting with a free platform like WordPress or go
    for a paid option? There are so many choices out there that I’m totally overwhelmed
    .. Any recommendations? Thanks a lot!

    Check out my homepage – free Download greatwall99

    Reply
  8. Excellent pieces. Keep posting such kind of info on your site.
    Im really impressed by your blog.
    Hi there, You’ve done an excellent job. I will definitely digg it
    and individually suggest to my friends. I’m confident they’ll be benefited from this web site.

    Feel free to surf to my blog: download game
    playboy2 (918kiss-m.com)

    Reply
  9. Hi, Neat post. There is a problem together with your website in internet explorer, could test
    this? IE still is the market chief and a good section of other folks will miss your magnificent
    writing because of this problem.

    Feel free to surf to my webpage – gamezombie.co.uk

    Reply
  10. Great blog here! Also your site loads up very
    fast! What web host are you using? Can I
    get your affiliate link to your host? I wish
    my web site loaded up as quickly as yours lol

    Reply
  11. That is obviously a very silly question, but also one quite worthy of this blog.
    The boxes move at the connecting points, sometimes around one axis, sometimes
    around more than one. That’s it: they are boxes. In the game,
    the animals look much nicer, as the boxes are depicted
    as smoothed forms with some nice fishy textures.
    In a way this is a pity, as otherwise the animals have nothing that reminds
    you of a vertebrate. In my searches for original means of locomotion to
    use on Furaha, I had tried to find a way to have a limb do just that.
    The problem is of course that in biology you cannot have a body
    part going through a complete 360 degree turn, as that would of be incompatible with blood vessels, nerves and muscles running to that limb.
    Another example is the animation above this paragraph: the animal seems to
    wave one limb in such a way that it acts as sort of
    propeller.

    Reply
  12. Saya telah telah berselancar online lebih dari 4 jam hari ini, namun saya tidak pernah menemukan artikel menarik
    seperti milik Anda. It is cukup berharga bagi saya. Menurut pendapat saya , jika semua
    webmaster dan blogger membuat konten yang baik seperti yang Anda lakukan, internet akan menjadi a jauh lebih berguna dari sebelumnya.

    Also visit my web site; Cara daftar joker388

    Reply
  13. Saya akan segera rebut rss feed Anda karena saya tidak bisa untuk menemukan layanan e-mail langganan link
    atau e-newsletter Anda. Apakah Anda punya? Mohon ijin saya mengerti agar saya bisa berlangganan. Terima kasih.

    Also visit my homepage: Game tembak ikan deposit (Marcy)

    Reply
  14. Saya akan segera pegang rss Anda karena saya tidak bisa
    Ini waktu tepat untuk membuat beberapa rencana untuk masa depan dan sekarang waktu untuk bahagia.

    Saya punya baca kirim ini dan jika saya boleh saja saya menasihati Anda beberapa menarik hal atau tips.
    Mungkin Anda bisa menulis artikel berikutnya mengenai artikel ini.
    Saya ingin membaca lebih lagi hal kira-kira
    itu!

    Here is my webpage; Situs joker kita (https://gameaco.com/www-joker123net-apk-download-untuk-menyenangkan)

    Reply
  15. Hi I am so grаateful I found your weblog, I really found you
    by accident, while I was researching on Aol for something else, Nonethеless I am here now and woulɗ
    just like to say cheers for а incredible post andd a alⅼ round exciting blog (I also
    love tһe themе/design), I don’t һave time to read through it аll at the minute but I have
    booқ-marҝed it and also included your RSS feeԁs, so when I have time I wjll be back tto read a great deal more, Plleasе do keep
    ᥙpp the awesome work. http://tyuratyura.s8.xrea.com/bbs/i-regist.cgi

    Reply
  16. I’d been honored to receive a call coming from
    a friend as soon as he observed the important ideas shared on the site.
    Examining your blog posting is a real brilliant experience.
    Thank you for thinking of readers at all like me, and
    I wish you the best of success like a professional in this surface area.

    Here is my website: voipxhub.com

    Reply
  17. Ηaving read tһis I believed it was really enlightening.
    I ɑppreciate you taking the time and energy to putt this
    infoгmation together. I once again find myself personally spending a lot of time bⲟth reading ɑnd pߋsting comments.
    Buut so what, it was still worth it!

    Reply
  18. Hi, I do think this is an excellent website. I stumbledupon it 😉 I may return yet again since I bookmarked it.
    Money and freedom is the greatest way to change, may you be rich and continue to help other people.

    Reply
  19. Greetings from Los angeles! I’m bored at work so I decided to check out your website
    on my iphone during lunch break. I enjoy the knowledge you provide here and can’t wait to take a look when I
    get home. I’m shocked at how fast your blog loaded on my cell
    phone .. I’m not even using WIFI, just 3G .. Anyways, awesome blog!

    Reply

Leave a Comment