user1232968 user1232968 - 5 months ago 19
jQuery Question

Set value of hidden field via Javascript

I'm trying to use Javascript to set the value of a hidden field.

I'm following an example that binds the value to a label thus:

var input = $("#mobile-number"),
output = $("#output"),
input.on("keyup change", function() {
var intlNumber = input.intlTelInput("getNumber");
if (intlNumber) {
output.text("International: " + intlNumber);
} else {
output.text("Please enter a number below");
}
});




That works, but I am trying to reproduce that by storing the value in a hidden field instead of displaying it in a label.

Answer

If your hidden field is named as output, just substitute text function by val function, like this:

var input = $("#mobile-number"),
  output = $("#output"),
  input.on("keyup change", function() {
    var intlNumber = input.intlTelInput("getNumber");
    if (intlNumber) {
      output.val("International: " + intlNumber);
    } else {
      output.val("Please enter a number below");
    }
  });