14

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?

5 Answers 5

16

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>
Sign up to request clarification or add additional context in comments.

4 Comments

Satish, the above code is not working. Wats the below code do, <a href='close()'>close</a>.. Why u are using "close()", what does it do?
first of all: inline javascript is a bad idea. second: using href to do javascript is even worse. bad, bad solution.
First time i gave a idea thats all. I dint tell to add script tag inline. Here is my edited script.
Thank you satish, This is working well. thanks and thanks for everyone for their valuable comments and answers...
14
<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>

2 Comments

there's no need to specify javascript with onclick events. <a onclick="this.innerHTML ... is sufficient.
@i2ijeya - that was just a basic example, i figured you could edit the HTML to suit your needs. i added href="" to the example
5

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";
};

3 Comments

InnerHTML is evil. It is better to use nodeValue.
@i2ijeya: e is the default parameter that is given to event-handling functions in FF, Opera and WebKit based browsers, an Event object. @silent: It is evil, but it is also foolproof. Using nodeValue involves testing if a firstChild exists.
You can, by the way call the 'e' whatever you like. Most tutorials go with 'event' here.
0

Say you have link:

<a id="link" href="https://www.example.com" onclick=change(event)>open</a>

Then you can use the following javascript code

const change = (event) => {
  const link = document.getElementById("link");
  link.text = link.text === 'open' ? 'close' : 'open';
  event.preventDefault();
}

So to change label value you can simply use the text property of the element:

link.text = 'closed'; // now the text on the href will be 'closed'

I will add a a fiddle to demonstrate the workings of this simple code.

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. This is merely a simple code example for demonstrating the working. You could for example also grab the anchor element from the event...


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

1 Comment

I edited my answer and added a fiddle demonstrating the code. Please do not downvote without giving any proper feedback. The answer is solid and end the text property on HTMLAnchorElement is exactly for the purpose of manipulating the label like OP requests..
-1

If elm is the link:

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

2 Comments

no, this will not work cross-browser. attachEvent is IE's method of attaching events, addEventListener works in all others as far as I'm aware
Using innerHTML is not considered good practice. See also comment from @silent under the other answer. It can be used for injecting malicious code into the dom. For setting the text label you can simply use the text property on the HTMLAnchorElement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.