Why does not it show the values?

0

I have the following object in javascript, but it does not show me any value, name, age, or purse, what is the error?

// El jugador
function Jugador(name, edad, dinero) {
  
  this.name = name;
  this.edad = edad;
  this.dinero = dinero;
   
}

function obtenerNombre() {  // Obtengo el nombre del input
  
  var obtenerN = document.getElementById("nombre").value;
  obtenerN.toString();
  
  return obtenerN;
  
}
function obtenerEdad() { // Obtengo la edad del input
  
  var obtenerE = document.getElementById("edad").value;
  obtenerE = parseInt(obtenerE);
  
  return obtenerE;
}
function crearJugador() { // Creo el jugador , pasando como parametro las funciones para obtener nombre y edad, y por ultimo 0 por defecto en el monedero.
  
  new Jugador(obtenerNombre(), obtenerEdad(), 0);
  document.write(Jugador.nombre, Jugador.edad, Jugador.monedero); // Escribo todos los datos del jugador
}
<!DOCTYPE html>
<html>
<head>
<title>HTML5, CSS3 and JavaScript demo</title>
</head>
<body>
<!-- Start your code here -->

  <style>
    body {
      
      background-color: #111111;
    }
   h1 {
      
      color: white;
  }
   
    
  </style>
  <form>
<input type="text" id="nombre" placeHolder="Nombre"/>
    <input type="text" id="edad" placeholder="edad"/>
    <input type="button" onClick="obtenerNombre(); obtenerEdad(); crearJugador();" value="Enviar mis datos!"/>
    
  </form>
  
  

<!-- End your code here -->
</body>
</html>
    
asked by Eduardo Sebastian 08.05.2017 в 19:05
source

2 answers

3

You have several errors in your code:

  • At the time you want to print the properties of your class Jugador there are two that are misspelled: Jugador.nombre the correct one is Jugador.name and Jugador.monedero the correct one is Jugador.dinero
  • You have to initialize your class within a variable so that you can access its attributes, otherwise to access them in the way you do will always return undefined values.
  • It is not necessary to send your methods of obtenerNombre and obtenerEdad in the click of the button, since that already you do it by means of the function crearJugador
  • obtenerN.toString() is over, because value already returns a string.

I leave the Snippet running, and already prints the values you capture from your form.

Greetings

function Jugador(name, edad, dinero) {
  
  this.name = name;
  this.edad = edad;
  this.dinero = dinero;
   
}

function obtenerNombre() {  // Obtengo el nombre del input
  
  var obtenerN = document.getElementById("nombre").value;
  return obtenerN;
  
}
function obtenerEdad() { // Obtengo la edad del input
  
  var obtenerE = document.getElementById("edad").value;
  obtenerE = parseInt(obtenerE);
  
  return obtenerE;
}

function crearJugador() {
  
  var jug = new Jugador(obtenerNombre(), obtenerEdad(), 0);
  document.write(jug.name, jug.edad, jug.dinero);
}
<!DOCTYPE html>
<html>
<head>
<title>HTML5, CSS3 and JavaScript demo</title>
</head>
<body>
<!-- Start your code here -->

  <style>
    body {
      
      background-color: #111111;
    }
   h1 {
      
      color: white;
  }
   
    
  </style>
  <form>
<input type="text" id="nombre" placeHolder="Nombre"/>
    <input type="text" id="edad" placeholder="edad"/>
    <input type="button" onClick="obtenerNombre(); obtenerEdad(); crearJugador();" value="Enviar mis datos!"/>
    
  </form>
  
  

<!-- End your code here -->
</body>
</html>
    
answered by 08.05.2017 / 19:21
source
0

There are several problems, the age id is age not name, it should be:

 <input type="text" id="edad" placeholder="edad"/>

You can only call the crearJugador() method instead of:

obtenerNombre(); obtenerEdad(); crearJugador();"

since crearJugador() calls the other methods.

You should check the variables that you use since they are not the same.

// El jugador
function Jugador(nombre, edad, dinero) {
  
  this.nombre = nombre;
  this.edad = edad;
  this.dinero = dinero;
   
}

function obtenerNombre() {  // Obtengo el nombre del input      
  var obtenerN = document.getElementById("nombre").value;
  return obtenerN;
  
}
function obtenerEdad() { // Obtengo la edad del input      
  var obtenerE = document.getElementById("edad").value;
  return  parseInt(obtenerE);    
}

function crearJugador() { // Creo el jugador , pasando como parametro las funciones para obtener nombre y edad, y por ultimo 0 por defecto en el monedero.
  
  var jugador = new Jugador(obtenerNombre(), obtenerEdad(), 0);
  document.write("nombre: " + jugador.nombre + " ,edad: " + jugador.edad + " , dinero: " + jugador.dinero); // Escribo todos los datos del jugador     
}
<!DOCTYPE html>
<html>
<head>
<title>HTML5, CSS3 and JavaScript demo</title>
</head>
<body>
<!-- Start your code here -->

  <style>
    body {
      
      background-color: #111111;
    }
   h1 {
      
      color: white;
  }
   
    
  </style>
  <form>
<input type="text" id="nombre" placeHolder="nombre"/>
    <input type="text" id="edad" placeholder="edad"/>
    <input type="button" onClick="crearJugador();" value="Enviar mis datos!"/>
    
  </form>
  
  

<!-- End your code here -->
</body>
</html>
    
answered by 08.05.2017 в 19:22