0
my html 
 
  <div class="container">
        <form action="">
            <div><tr>
                <label for="input1"> Password length:</label>
                <input type="number" id="passwordlen">
                <button onclick="Password()">submit</button></tr>
            </div>
            <div><tr>
                <p>Password</p>  
                <div> <input type="text" id="passgen"> </div></tr>
            </div>
        </form>
    </div>

my javascript

var strings = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$&"

// var passlen = document.getElementById('input1').value
// passlen = parseInt(passlen)
// console.log(passlen)

function Password() {
  console.log("abc")
  var passlen = document.getElementById("passwordlen").value
  console.log(passlen)

  var password = '';
  for (i = 0; i <= passlen - 1; i++) {
    let rand_num = Math.floor(Math.random() * 66);
    password = password + strings[rand_num];

    

  }
  console.log(password)
  document.getElementById('passgen').value = password;
}

when i click submit it prints on console as well as screen but also gets refreshed so results are not visible. There is no error message. Also i tried to add script tag before and once before .

Jay Sardar
  • 39
  • 1
  • 8
  • 1
    submit button submits the form, which by default refreshes the page. use `submit` event listener and call `preventDefault()` – boxdox Aug 23 '21 at 10:14

1 Answers1

1

The default button type is submit which causes the page to refresh.

replace

<button onclick="Password()">submit</button>

with

<button type="button" onclick="Password()">submit</button>
thedude
  • 9,388
  • 1
  • 29
  • 30