9

In angularjs,I want to get the value of the hidden input. such as the following:

<pre>
<input type="hidden" name="captId" value="AqXpRshs9QHfxUbOWqMT" ng-model="captId">
</pre>

How to get the "hidden input" value "AqXpRshs9QHfxUbOWqMT" by using angularjs,not ajax or jquery.

2
  • 1
    document.getElementById("captId").value Commented Mar 3, 2014 at 7:24
  • it's far better using an input type text with display:none Commented Mar 3, 2014 at 8:05

2 Answers 2

24

You can use ng-init to initialize the value so it binds that to your model variable upon creation.

<input type="hidden" name="captId" ng-init="captId='AqXpRshs9QHfxUbOWqMT'" ng-model="captId">

Or you can get it directly if all else fails:

angular.element(document.getElementsByName('captId')[0]).val();
// or dont use angular at all
document.getElementsByName('captId')[0].value

Documentation for angular.element

Sign up to request clarification or add additional context in comments.

2 Comments

the second way you mention is much better
@PascalLeMerrer agreed, I think if op was to use the first method he/she might find that the value is set to an empty string because it would be overwritten with the captID value which is likely blank.
1

By ID

document.getElementById('yourId').value

By Name

document.getElementsByName('yourName')[0].value

Keep it simple :)

1 Comment

Your by name should be: document.getElementsByName('yourName')[0].value since since the getElementsByName returns a collection

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.