Get input value

-2

I would like to know how to show the value of the input in an alert, immediately at the moment that I finish writing inside the input that shows it to me. In JQuery

function mostrar(num) {
 var x = $("#num").val();
 alert(x);
}
    <input type="text" name="num" step="1" Onchange = "mostrar('num')" value=""  size="20"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
asked by Oriol 29.10.2017 в 14:17
source

3 answers

1

I would do it in the following way, taking advantage of the jQuery library.

$('#num').focusout(function() {
 var x = $(this).val();
 alert(x);

 // Recomiendo usar la consola en lugar de alerts
 console.log(x);
});
    <input type="text" name="num" step="1" id="num" value=""  size="20"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
answered by 29.10.2017 в 17:31
0

Well, I think there are mixed concepts and some errors in the example of both html and jquery, so I've edited it. Still, literally following your instructions.

function mostrar() {
 var x = $("#numero").val();
 alert(x);
}
<input type="number" step="1" onkeyup="mostrar()" id="numero" size="20"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

It's annoying, does not it? you could indicate if you need some timeout or wait for a specific event to occur such as onBlur, onChange ...

** Update **

your example being the following:

function mostrar(num) {
 var x = $("#num").val();
 alert(x);
}
    <input type="text" name="num" step="1" Onchange = "mostrar('num')" value=""  size="20"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

there are still errors in your code, even so:

function mostrar(input) {
 //solo queremos mostrar la alerta
 // si tengo un formato en concreto
 // por ejemplo dos numeros separados por un espacio
 if(/[0-9]+ [0-9]+/.test(input.value)){
   var numeros = input.value.split(' ');
   alert(numeros[0]+','+numeros[1]);
 }
 //o con jquery usando this
 if(/[0-9]+ [0-9]+/.test($(input).val())){
   var numeros = $(input).val().split(' ');
   alert(numeros[0]+','+numeros[1]);
 }
}
<p>
onchange se ejecuta al perder el focus o al cambiar de valor solo en campos select, radio y checkbox <br>
<input type="text"  onchange="mostrar(this)"/>
</p>

<p>
onkeyup se ejecuta justo despues de cada introduccion o eliminacion de caracteres <br>
<input type="text"  onkeyup="mostrar(this)"/>
</p>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
answered by 29.10.2017 в 15:24
0

You can only achieve it with javascript taking into account:

let num = document.getElementById('num'); 
num.addEventListener("keyup", function(){
  alert(this.value);
});
<input type="text" id="num" name="num" step="1"  value=""  size="20"/>

With jQuery:

$('#num').on("keyup", function(){
	alert(this.value);
});
<input type="text" id="num" name="num" step="1"  value=""  size="20"/>
 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
answered by 29.10.2017 в 19:27