Copy text from a tag and paste into a field by pressing a button

Asked

Viewed 699 times

2

Good afternoon everyone! Guys I have a custom page of a captive portal on a pfsense server. On this page there are filters in php where page elements are different depending on the result that the filter in php brings, in which case the filter is based on ips and mac address for employees and visitors. Currently when visitors enter the wifi the portal page appears and there is a button where the visitor clicks and the voucher code is automatically copied from a tag span (the code is entered in the tag span by php, something else!) and then the person has to paste the code into the entry field of the voucher on the login page press the connect button. This has already been done, I will put Cód here, however I would like to leave more automated rsrsrs for when the person click on the button to copy the voucher the copied code already went to the input field of the voucher code and also already sent the form for connection. Follow the current well simplified code:

Button for visitor click and copy the voucher Cód:

click <a id="button2" href="#inp_voucher" style="color: #fdc501">HERE</a>

Tag with id 'Cod-voucher' that receives the Cód pfsense php voucher:

<span id="cod-voucher" style="display: none">salsifrufru</span>

Code that copies the #Cod-voucher span tag voucher when the #button2 button is pressed:

<script>
        $(document).ready(function(){
            $("#button2").click(function(){
                var texto = $("#cod-voucher").text();
                var $temp = $("<input>");
                $("body").append($temp);
                $temp.val(texto).select();
                document.execCommand("copy");
                $temp.remove();
                alert ("Código copiado! Agora cole no campo 'Código Voucher' e precione 'CONECTAR!'");
                });
        })
</script>

Input where the visitor should paste the voucher code copied above, note that the input is with ip filter by php because for those who have no visitor ip address (employees should not use voucher) this field is disabled:

<?php
    $ip = $_SERVER["REMOTE_ADDR"];
        if($ip=="192.168.0.95") {echo "<input name=\"auth_voucher\" type=\"text\" id=\"voucher\" placeholder=\"Voucher somente para visitantes!\" readonly=\"true\">"; }
        else { echo "<input name=\"auth_voucher\" type=\"text\" id=\"voucher\" placeholder=\"Código Voucher\">"; }
?>

Button "Submit" to connect after paste the voucher code:

<div><button name="accept" type="submit" value="continue" class="submitform">CONECTAR!</button></div>

1 answer

0


If I understand correctly, just do so:

<script>
        $(document).ready(function(){
            $("#button2").click(function(){
                var texto = $("#cod-voucher").text();
                var $temp = $("<input>");
                $("body").append($temp);
                $temp.val(texto).select();
                document.execCommand("copy");
                $temp.remove();

                $('#voucher').val(texto);
                });
        })
</script>

I just added up $('#voucher').val(texto); within its function. What this line does is to place the contents of the variable texto within the value for the element with id = voucher

  • 1

    That’s right, Otavio, thank you. Then I only had to use a function to make q the same button when clicked also trigger the connect button in the form. Thanks.

Browser other questions tagged

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