0

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.

4
  • 3
    if the element identified with id output were an input with type hidden, you can use output.val() instead of output.text() Commented Jul 2, 2016 at 13:41
  • also change the comma after output = $("#output"), to a semicolon Commented Jul 2, 2016 at 13:42
  • 1
    html: <input id="output" type="hidden" name="output"> and js: output.val("your text"); (just as @daf suggested, if you didn't understand that..) Commented Jul 2, 2016 at 13:50
  • Thank You Guys for the guidance, Issue's been Resolved Commented Jul 2, 2016 at 14:23

1 Answer 1

1

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");
    }
  });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.