I've asp web site , I used master page for the design. I've child page which is placed in the contentplaceholder. On the child page i used one hidden field as -
<input id="Hidden1" type="hidden" value="This is hidden text"/>
I want to display the hidden field value using alert() function from javascript on the page load event. How to do this?
I tried following thing in my script but it is not working-
(function msgShow() {
var e1 = document.getElementById('Hidden');
alert(e1.value);
})();
Thanks.
I've asp web site , I used master page for the design. I've child page which is placed in the contentplaceholder. On the child page i used one hidden field as -
<input id="Hidden1" type="hidden" value="This is hidden text"/>
I want to display the hidden field value using alert() function from javascript on the page load event. How to do this?
I tried following thing in my script but it is not working-
(function msgShow() {
var e1 = document.getElementById('Hidden');
alert(e1.value);
})();
Thanks.
Share Improve this question edited Jul 19, 2012 at 12:04 RAS 8,15017 gold badges66 silver badges86 bronze badges asked Dec 2, 2011 at 9:06 PriyankaPriyanka 2,83214 gold badges57 silver badges89 bronze badges 6- What doesn't work exactly? You get an error, null value, undefined... – Viruzzo Commented Dec 2, 2011 at 9:16
- @Viruzzo: I don't get any error. I just display my page without the alert message. – Priyanka Commented Dec 2, 2011 at 9:20
-
If you put an
alert('Hello World!');
before the getElementById, does it show? – Viruzzo Commented Dec 2, 2011 at 9:29 - 1 Your hidden element should be there when you call your msgShow function e.g call your function after DOM is fully loaded. – bsrykt Commented Dec 2, 2011 at 9:30
- 1 @Virusso:yes alert('hello world') is working – Priyanka Commented Dec 2, 2011 at 9:39
8 Answers
Reset to default 1window.alert(document.getElementById("Hidden1").value);
Make sure this code is executed after the DOM is ready.
With jQuery you do like this:
$(document).ready(function() {
alert($('#Hidden1').val());
});
without jQuery you do:
alert(document.getElementById('Hidden1').value);
Just like with any other element, you can get it with document.getElementById('Hidden1').value
Refer the code given below to know how to get
<html>
<body>
<script type="text/javascript">
function printIt(){
alert(document.getElementById('abcId').value);
alert(document.formName.elements['abcName'].value);
}
</script>
<h1>Access Hidden value in JavaScript</h1>
<form name="formName">
<input type="hidden" id="abcId" name="abcName"
value="I am Hidden value"/>
<input type="button" value="Get Value" onclick="printIt()" />
</form>
</body>
</html>
document.getElementById('Hidden1').value;
and alert the return value
<script type="text/javascript">
function dis() {
var j = document.getElementById("<%= Hidden1.ClientID %>").value;
alert(j);
}
</script>
<input id="Hidden1" type="hidden" runat="server" value="Hello" /><br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return dis();" />
With pure JavaScript:
var value = document.getElementById(id).value;
Also be sure not to reference a DOM element before it exists - like I just did and spent an hour trying to figure why even HelloWorld would not work.