How to create a link that when clicked changes the value of href=""?

1

I'm creating a button to open chat on one of my sites. I'm currently using Zopim.

To open the chat, the link is used:

<a href="javascript:void($zopim.livechat.window.show())">ABRIR CHAT</a>

To close the chat, the link is used:

<a href="javascript:void($zopim.livechat.window.hide())">FECHAR CHAT</a>

I need when to click on Abrir o Chat , the link automatically changes to Fechar o Chat and that when the person clicks Fechar o Chat change to Abrir o Chat .

That is, it keeps switching, when you click on one the link changes to the other.

So with the same button I can open and close the chat.

    
asked by anonymous 12.08.2014 / 04:01

2 answers

4

There are several ways to do this. I would avoid inline JavaScript (within the href or other attribute). Something like this would be cleaner:

<a id="botao-chat" href="#">ABRIR CHAT</a>
<script>
var aberto = false;
$('#botao-chat').click(function() {
    if(aberto) {
        $(this).text('ABRIR CHAT');
        $zopim.livechat.window.hide();
    } else {
        $(this).text('FECHAR CHAT');
        $zopim.livechat.window.show();
    }
    aberto = !aberto;
    return false;
});
</script>
    
12.08.2014 / 04:34
0

Hello friend to change the value of href in the click use the property setAttribute

see an example:

<a href="#" id="AbrirChat">ABRIR CHAT</a>
<a href="#" id="FecharChat">FECHAR CHAT</a>

<script type="text/javascript">
   document.getElementById("FecharChat").onclick = function() {
   var link = document.getElementById("AbrirChat");
   link.setAttribute("href", "abrir.html");
   return false;
   }
</script>

In this example the href property of OPEN CHAT changes as soon as you click CLOSE CHAT

    
12.08.2014 / 04:30