You are viewing limited content. For full access, please sign in.

Question

Question

Javascript: fill value after clicking on button

asked on February 3, 2016 Show version history

Hello,

I want to create Random Password Generator, I found the following on net:

function generatePassword() {
    var length = 8,
        charset = "abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
        retVal = "";
    for (var i = 0, n = charset.length; i < length; ++i) {
        retVal += charset.charAt(Math.floor(Math.random() * n));
    }
    return retVal;
}

 

My Button has the following attribute: <input id="Generate Password" onclick="generatePassword(this.value)" type="button" value="Generate Password">

The id of the field is: q12

 

The id of Text Field is: q11

 

I want, when the user hits the button, a random password should be populated in the text field q11.

 

Please help.

Regards,

S

0 0

Answer

SELECTED ANSWER
replied on February 3, 2016

I would lose the "this.value" parameter in your onclick callback. Just use "generatePassword()" since it doesn't accept any parameters. Second, you should replace the return statement at the end of your function with

$("q11 input").val(retVal);

That should do it.

0 0

Replies

replied on February 3, 2016

I changed it to:

 

function generatePassword() {
    var length = 8,
        charset = "abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
        retVal = "";
    for (var i = 0, n = charset.length; i < length; ++i) {
        retVal += charset.charAt(Math.floor(Math.random() * n));
    }
    $("q11 input").val(retVal);
}

 

However, it doesn't fill the text box, also I removed this.value....

0 0
replied on February 3, 2016

I figured it out, it was  $("#q11 input").val(retVal);

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.