Switching the text of a link

Discussion in 'JavaScript' started by tirengarfio, Jan 28, 2010.

  1. #1
    Hi,

    i have this code below. When it is loaded it shows a link with the text "Estado 1". When you click it, it should be replaced by another link with the text "Estado 2". When you click on "Estado 2" the link "Estado 1" should appear again and so on...


    	<script type="text/javascript">
    		function cambio(){
    			var capa = document.getElementById("estado1");
    			if (capa.id == "estado1")
                                {
                                    capa.innerHTML = <a href="#" onclick="cambio()">Estado 2</a>;
    
                                    capa.id = "estado2";
                                }
                            else{
                                   capa.innerHTML = <a href="#" onclick="cambio()">Estado 1</a>;
                                   capa.id = "estado1";
                            }
                            //capa.innerHTML= (capa.style.display == "none") ? "block" : "none";
    
                           
    		}
    	</script>
    </head>
    <body>
    	<div>
    
    		<div id="estado1">
    		          <a href="#" onclick="cambio()">Estado 1</a>
    		</div>
    
    	</div>
    HTML:

    My problem: after clicking "Estado 1" the sentence "Estado 2" is showed, but not as a link but as plain text..

    Javi
     
    tirengarfio, Jan 28, 2010 IP
  2. Joak1m

    Joak1m Peon

    Messages:
    135
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Joak1m, Jan 28, 2010 IP
  3. fundoobuddy

    fundoobuddy Peon

    Messages:
    274
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here You Go ::

    <script type="text/javascript">
            function cambio(){
                var capa = document.getElementById("estado1");
    		if(capa == null)
    		var capa = document.getElementById("estado2");
    
                if (capa.id == "estado1")
                                {
                                    capa.innerHTML= '<a href="#" onclick="cambio()">Estado 2</a>';
    
                                    capa.id = "estado2";
                                }
                            else{
                                   capa.innerHTML = '<a href="#" onclick="cambio()">Estado 1</a>';
                                   capa.id = "estado1";
                            }
                            //capa.innerHTML= (capa.style.display == "none") ? "block" : "none";
    
                           
            }
        </script>
    </head>
    <body>
        <div>
    
            <div id="estado1">
                      <a href="#" onclick="cambio()">Estado 1</a>
            </div>
    
        </div>
    Code (markup):
     
    fundoobuddy, Jan 28, 2010 IP
  4. tirengarfio

    tirengarfio Greenhorn

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    Thanks it worked!
     
    tirengarfio, Jan 29, 2010 IP