Problem to get input value with javascript

2

I'm getting the value of input through JS and sending via GET to another page. The problem is that when I type space inside input , I can not get what I wrote after the space.

If I type for example: Ana Maria JS just takes the Ana.

HTML:

<input type="text" id="nome" onkeyup="pegaNome()">
<div id="aqui"></div>

The JS function:

 function pegaNome(){

    var campo=document.getElementById('nome').value;
    $(document).ready(function(){
    $('#aqui').append(campo);

});
    
asked by anonymous 09.03.2016 / 20:00

2 answers

2

You can do this:

function pegaNome(valor) {
  document.getElementById("aqui").innerHTML = valor;
}
<input type="text" id="nome" onkeyup="pegaNome(this.value);">
<div id="aqui"></div>
    
09.03.2016 / 20:21
4

Tip or pure JQuery , it becomes easier to understand logic. (In your example there was jQuery mixing with pure Javascript)

Below is your example working in Jquery.

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script><script>$(document).ready(function(){$("#nome").keypress(function(){
        var valor = $("#nome").val();
        $("#aqui").text(valor);
    });
});
</script>
</head>
<body>

<input type="text" id="nome">
<div id="aqui"></div>

</body>
</html>

In pure javascript:

 <html>
    <head>
    <script>
    function peganome(valor) {
      document.getElementById("aqui").innerHTML = valor;
    }
    </script>
    </head>
    <body>
    <input type="text" id="nome" onkeyup="peganome(this.value);">
    <div id="aqui"></div>
    </body>
    </html>

In AngularJS (without jquery or any other plugin)

<html ng-app>
  <head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script></head><body><div><label>Nome:</label><inputtype="text" ng-model="nome">
      <hr>
      <h1>Olá {{nome}}!</h1>
    </div>
  </body>
</html>
  

NOTE: In AngularJS is the easiest basically is a ng-model="name" and   then to display is {{name}}, but use AngularJS only for   this is like having a Ferrari to just go to the bakery.

    
09.03.2016 / 20:21