I want to declare a javascript variable by INPUT TEXT

Asked

Viewed 140 times

0

I would like to do the inverse of this script, in this script you click the GENERATE button, it generates 4 digits

What I want to do is the reverse, I want to declare the variable "1298105" (or any other) and click on Generate and it generate below in one <div> the result "1298105XXXX" as declared in input

How to do, some example ?

            function gerar() {

                // numero aleatorios
                var numeros = [];
                for (var i = 0; i < 4; ++i) {
                    var sorteio = Math.floor(Math.random() * 9);

                    if (sorteio == 0)
                        sorteio = 1; // 1 - 9

                    numeros[i] = sorteio; 
                }

                var senha = numeros.join('');

                document.getElementById('txtsenha').value = senha;
            }
        <input type="text" name="txtsenha" id="txtsenha"/>
        <input type="button" value="Gerar" onclick="javascript:gerar()"/>

1 answer

0


function gerar() {
                // numero aleatorios
                var numeros = [];
                for (var i = 0; i < 4; ++i) {
                    var sorteio = Math.floor(Math.random() * 9);

                    if (sorteio == 0)
                        sorteio = 1; // 1 - 9

                    numeros[i] = sorteio; 
                }

                var senha = numeros.join('');


    var x = document.getElementById("txtsenha").value;
    document.getElementById("demo").innerHTML = x+senha;
}
<input type="text" name="txtsenha" id="txtsenha"/>
<input type="button" value="Gerar" onclick="javascript:gerar()"/>
<div id="demo"></div>

  • That’s right Leo, Thanks for the reply , take a look at my profile, I’m having another problem with javas ( already searched solution on google and not found ), if you can show me the way I’m grateful.

Browser other questions tagged

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