0

I'm making a code where the user enters information and it stores it in an object, but when I check if the object is saved by alerting one of its values it says there is no such object. here is my code.

let users = {};

function user(name,real){
  this.username = name,
  this.realname = real,
  this.id = Math.floor(Math.random() *1000);
  this.subs = 0,
  this.videos = [],
  this.listuser = function() {
    return this.username;
  }
};
function newuser(name, email,username){
  users[name] = new user(username, name);
};


let savefile = () => {
        
  var channame = string(document.getElementById('channame'));
  var name = string(document.getElementById('name'));
  var email = string(document.getElementById('email'));
  newuser(name,email,channame);
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>youtube ripoff</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
  <br> <br><br><br><br><br>
  <form action="">
  <label for="username">Channel Name: </label>
  <input type="text" id="channame" name="channelname"><br>
  Real Name
  <input type="text" id="name" name="name"><br>
  Email
  <input type="text" id="email" name="email"><br>
  <input type="submit" value="Submit" onmousedown="newuser('name1','name@gmail.com','channelname1');">
  <br><br><br><br>
  
  <input type="button" value="Load Channels" id="seech" name="seechannels" onmousedown="alert(users['name1'].listuser());"><br>
  </form>
    <script src="script.js">
    function fun(){
      window.alert("starting program...")
      //other code
    }
    </script>
  </body>
</html>

Is there some sort of error that I'm not seeing?

haha2567
  • 7
  • 2
  • 3
    When you use ` – Sebastian Simon Aug 20 '21 at 18:18
  • The `this` inside the `listuser` function isn't what you think it is. Probably undefined as it is called from mousedown handler. – Garr Godfrey Aug 20 '21 at 18:25

1 Answers1

0

After click on submit button in form with action="" page is reload and all data is lost. Pleas use:

<form action="javascript:void(0)">

instead of

<form action="">

This prevent page reload.

thie
  • 1
  • 6