2

I wrote a javascript program to generate random passwords. How can I output the result into a textfield (input tag)?

var randomString = function(length) {
  var text = "";
  var possible = "!@[|^]µ§$%&?*€#ABCDEFGHIJKLMNOPQRSTVWXYZabcdefghijklmnopqrstvwxyz1234567890";
  for (var i = 0; i < length; i++) {
    text += possible.charAt(Math.floor(Math.random() * possible.length));
  }
  return text;
}

var rs = randomString(16);
console.log(rs);
<section id="passgen">
  <button type="button" id="pass-button" onclick="**???**">Generate Password</button>
  <br><br>
  <input type="text" name="output">
</section>

3
  • W3Schools has great tutorial on how to write onclick functions or manipulate DOM elements... Commented Jul 5, 2017 at 9:30
  • set id for input tag, using javascript access the input filed and set the value Commented Jul 5, 2017 at 9:31
  • I think for a beginner it's better if you watch video tutorials about javascript manipulate DOM element(s) Commented Jul 5, 2017 at 9:31

5 Answers 5

4

It's not a good idea to add an inline click event, you can use addEventListener instead.

So here, I am first selecting the element using document.getElementById and attaching a click event for the same, and later, using document.getElementsByName() I select the output element and print the randomized string.

Also note the [0] which am using with document.getElementsByName() is because if the code encounters multiple elements with the name of output, we select the first one, as it returns an array of matched elements. It will be great if you can add some id to the input field and then use document.getElementById() to select a specific element.

var randomString = function(length) {
  var text = "";
  var possible = "!@[|^]µ§$%&?*€#ABCDEFGHIJKLMNOPQRSTVWXYZabcdefghijklmnopqrstvwxyz1234567890";
  for (var i = 0; i < length; i++) {
    text += possible.charAt(Math.floor(Math.random() * possible.length));
  }
  return text;
}

document.getElementById('pass-button').addEventListener('click', function() {
  document.getElementsByName('output')[0].value = randomString(16);
});
<section id="passgen">
  <button type="button" id="pass-button">Generate Password</button>
  <br>
  <br>
  <input type="text" name="output">
</section>

Sign up to request clarification or add additional context in comments.

1 Comment

Great explanation !! :)
1

you can do something like this:

var randomString = function(length) {
        var text = "";
        var possible = "!@[|^]µ§$%&?*€#ABCDEFGHIJKLMNOPQRSTVWXYZabcdefghijklmnopqrstvwxyz1234567890";
        for(var i = 0; i < length; i++) {
            text += possible.charAt(Math.floor(Math.random() * possible.length));   
        }
        return text;
    }

    var rs = randomString(16);

    console.log(rs);

    document.getElementsByName('output')[0].value = rs;

1 Comment

jQuery for this, really?
0

Very simple document.getElementById("id_of_input_field").value = rs; or else you can use name of input box to target document.getElementsByName('output')[0].value = rs;

Comments

0

var randomString = function(length) {
        var text = "";
        var possible = "!@[|^]µ§$%&?*€#ABCDEFGHIJKLMNOPQRSTVWXYZabcdefghijklmnopqrstvwxyz1234567890";
        for(var i = 0; i < length; i++) {
            text += possible.charAt(Math.floor(Math.random() * possible.length));   
        }
        return text;
    }

var generatePassword = function(length) {
document.getElementById('password').value = randomString(length);
}
<section id="passgen">
<button type="button" id="pass-button" onclick="generatePassword(16)">Generate Password</button>
<br><br>
<input id='password' type="text" name="output">
</section>

Comments

0

Give your input an ID, then:

document.getElementById('yourInputID').value = rs;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.