javascript does not return when concatenating link

Asked

Viewed 50 times

0

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

        <?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="javascript:void(0);>Seguir</a></li>';

            }

        }

        </script>
  • This correct is depends on the support php version, and try <? echo $user_id; ?>.

  • @Kingrider is no problem with PHP ,as I ran a test using the alert("user_id"); and it was successful

  • 1

    ok and but already solved? if you need help me skype chat live:Save... test code so alert(user_id) or console.log(user_id) rss.. see comment said @lvcs below.

  • All right here, thanks!

1 answer

1

Adjust only the id in html (onclick=...) for '<?=$user_id;?>'. 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 advantage a little of what you 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>

  • No @Php. I still have a solution that for me, considering the functionality is better. Do only one function instead of two.

  • 2

    Many servers do not have the option to use = to print data and tags <? ?> make it difficult to read. I suggest <?php echo $user_id; ?>

  • 1

    Yes, it’s true. However, that wasn’t the problem

  • Yes, I only commented to record :) because I saw in the reply and comment upstairs

  • Thank you @Ivcs :)

  • Simply perfect!

  • 1

    hehe @Php Obgado

  • @Miguel an observation, I always used this my method to send links dynamically, now with your method became simpler

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.