Sify Juhy
Sify Juhy

Reputation: 177

javascript getelementbyID does not return value

I am using javascript to get the value of a hiddenfield

<script type="text/javascript">
        $(function() {
            var newYear = document.getElementById('HF');
            alert('hehe' + newYear);
            $('#countdown').countdown({ until: newYear, format: 'DHMS', layout:
'<div id="timer">' + '<hr />' +
    '<div id="timer_days" class="timer_numbers">{dnn}</div>' +
    '<div id="timer_hours" class="timer_numbers">{hnn}</div>' +
    '<div id="timer_mins" class="timer_numbers">{mnn}</div>' +
    '<div id="timer_seconds" class="timer_numbers">{snn}</div>' +
'<div id="timer_labels">' +
    '<div id="timer_days_label" class="timer_labels">days</div>' +
    '<div id="timer_hours_label" class="timer_labels">hours</div>' +
    '<div id="timer_mins_label" class="timer_labels">mins</div>' +
    '<div id="timer_seconds_label" class="timer_labels">secs</div>' +
'</div>' +
'</div>'
            });
        });
</script>

This is the aspx code

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div class="maindeal">
            <input type="hidden" runat="server" id="HF" />
                <div class="SocialNetworkShare">
                    <asp:Label ID="SocialNetworkShare" runat="server" Text="Share" CssClass="SNS"></asp:Label>
                </div>

Code Behind[I am setting the value for the hidden field in code behind]

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            ShowOfferDetails(System.Configuration.ConfigurationManager.AppSettings.Get("DefaultOffer"), True)          '... here "0;0;0" is given as there is no event argument
        End If
        HF.Value = "10/21/2011"
        MsgBox(HF.Value)
    End Sub

The problem is javascript doesnot detect this value...and throws null exception when i use alert() to check the value.....please help.

Upvotes: 0

Views: 687

Answers (2)

KV Prajapati
KV Prajapati

Reputation: 94635

Try,

 var newYear = document.getElementById('<%=HF.ClientID %>'); //It return Html object
 alert(newYear.value);

 //OR

 var newYear1 = document.getElementById('<%=HF.ClientID %>').value;
 alert(newYear1);

Upvotes: 1

Ankur
Ankur

Reputation: 33637

Your HF control is server side and hence its ID in DOM will not be just HF. What you can do is put a class="HF" attribute in the HF control and then from JS you can do $(".HF").val() to get its value

Upvotes: 2

Related Questions