0

I'm trying to save the data of fields in simple vars. I only need the input values

<input type="text" id="login_email" class="large" name="login_email" value="">

<input type="submit" name="submit.x" value="Entrar" class="btn large">

How can I get data of login_email box and save it into a var?

Edit: The problem: the elementid "login_email" is static. I need some that when the user types the text the code captures it and save it into a var.

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Enrique García
  • 106
  • 1
  • 2
  • 11
  • If it's a form, jQuery has `$('form').serialize()` that gets everything for you ready to be sent – adeneo May 01 '15 at 20:16

2 Answers2

1

You can use the value property and the change event of HTMLInputElement:

var loginEl = document.getElementById('login_email');
var loginEmail;

loginEl.onchange = function emailChanged() {
    loginEmail = loginEl.value;
}
lxe
  • 6,661
  • 2
  • 19
  • 31
0

In order to capture what's being entered in an input, with jQuery, you can bind to the .keyup() event and save the value to a variable in the global / higher scope (eg outside of the event, or document.ready()):

Example:

var email;
$('#login_email').keyup(function(e) {
  email = $(this).val();
  $('#results').html(email);
});
#results {
  margin-top: 15px;
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="login_email" />

<span id="results"></span>
wahwahwah
  • 3,123
  • 1
  • 19
  • 37