javascript does not return to concatenate link

0

I have a link that changes dynamically, the problem is when I do the concatenation does not set anything up

        <?php
            $user_id = 1;
        ?>


        <div id="link-bt">
            <a href="javascript:void(0);" onClick="seguir('link-bt','Seguir','<?=$user_id;?>');">Seguir</a>  
        </div>


        <script type="text/javascript">

        function seguir(id,valor,user_id){

            var insert = document.getElementById(id);

            if(valor == "Seguir"){

                insert.innerHTML = '<a onClick=cancelar("link-bt","Cancelar","'+user_id+'"); href="javascript:void(0);>Cancelar</a></li>';

            }

        }
        function cancelar(id,valor,user_id){

            var insert = document.getElementById(id);

            if(valor == "Cancelar"){

                insert.innerHTML = '<a onClick=seguir("link-bt","Seguir","'+user_id+'"); href="#">#                    
asked by anonymous 21.06.2016 / 07:21

1 answer

1

Set only id in html (% with%) to% with%. I think this is what you want

<div id="link-bt">
            <a href="javascript:void(0);" onClick="seguir('link-bt','Seguir', 5);" id="my_link">Seguir</a>  
        </div>


        <script type="text/javascript">

        function seguir(id,valor,user_id){

            var insert = document.getElementById('my_link');

            if(valor == "Seguir"){

                insert.innerHTML = 'Cancelar';
                insert.onclick = function() {
                    cancelar("link-bt","Cancelar", user_id);
                }
            }

        }
        function cancelar(id,valor,user_id){

            var insert = document.getElementById('my_link');

            if(valor == "Cancelar"){

                insert.innerHTML = 'Seguir';
                insert.onclick = function() {
                    seguir('link-bt','Seguir', user_id);
                }
            }

        }

        </script>

To do just this feature and take a little of what it did, I would do so:

<div id="link-bt">
    <a href="javascript:void(0);" onClick="mudar_attrs('link-bt','Seguir', 5);" id="my_link">Seguir</a>  
</div>

<script type="text/javascript">

    function mudar_attrs(id,valor,user_id){
        var insert = document.getElementById('my_link');
        if(valor == "Seguir"){
            insert.innerHTML = 'Cancelar';
            insert.onclick = function() {
                mudar_attrs("link-bt","Cancelar", user_id);
            }
        }
        else {
            insert.innerHTML = 'Seguir';
            insert.onclick = function() {
                mudar_attrs("link-bt", "Seguir", user_id);
            }
        }
    }

</script>
    
21.06.2016 / 08:47