How to get the hidden input's value by using angularjs?
Asked Answered
C

2

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.

Cheltenham answered 3/3, 2014 at 7:19 Comment(2)
document.getElementById("captId").valueConcoction
it's far better using an input type text with display:noneBeauteous
D
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

Decury answered 3/3, 2014 at 7:25 Comment(2)
the second way you mention is much betterVallation
@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.Decury
F
1

By ID

document.getElementById('yourId').value

By Name

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

Keep it simple :)

Flack answered 27/4, 2015 at 11:58 Comment(1)
Your by name should be: document.getElementsByName('yourName')[0].value since since the getElementsByName returns a collectionUncap

© 2022 - 2024 — McMap. All rights reserved.