Reputation: 839
I am assigning the value to hidden field and I want to access that value in the JavaScript.
My declarative part is:
<asp:HiddenField ID="chkImages" runat="server" />
<div id="main" runat="server" style="display:none;"></div>
<a id="NewsTitle" runat="server" class="specialNews-label"></a>
My JavaScript code is:
$(document).ready(function () {
$( "#bxsliderid" ).parent().css( "display", "block" );
$('.bxslider').bxSlider({
controls:false,
pager:false,
auto:true,
speed: 9000,
easing: 'linear',
mode: 'fade',
pause: 5000,
});
if(document.getElementById('<%= chkImages.ClientId%>').value == "1") {
$( ".specialNews-label" ).css( "display", "block" );
}
});
I opened the page and see in Firebug that the hidden field is rendered but I am not getting the value of it, while debugging the JavaScript I am getting error like: Unable to get property 'value' of undefined or null reference
What am I missing?
Upvotes: 2
Views: 2844
Reputation: 3962
You not need to change any thing in your HTML code you just change your javascript as follow:
In your Jquery code:
$(document).ready(function () {
$( "#bxsliderid" ).parent().css( "display", "block" );
$('.bxslider').bxSlider({
controls:false,
pager:false,
auto:true,
speed: 9000,
easing: 'linear',
mode: 'fade',
pause: 5000,
});
if($("input[type='hidden'][id$='chkImages']").val() == "1") {
//alert('test');
$( ".specialNews-label" ).css( "display", "block" );
}
});
See Demo for your rendered HTML :
Upvotes: 1
Reputation: 5435
Use .attr('value')
in your code instead of .value
. Since you appear to be using jQuery, you can do this.
$(document).ready(function () {
if ($('#chkImages').attr('value') == "1") {
alert("Working...");
}
});
Upvotes: 0
Reputation: 10896
check id is same in my case it is my_id
js
document.getElementById('my_id').value == "1"
html
<input name="image_name" id="my_id" type="hidden" value="1"/>
Upvotes: 0
Reputation: 7591
try this
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyForm.aspx.cs" Inherits="TestApp.MyForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="hdnField" runat="server" />
</div>
</form>
<script type="text/javascript">
$(document).ready(function () {
alert($("#<%= hdnField.ClientID %>").val());
});
</script>
</body>
</html>
Upvotes: 0
Reputation: 973
please try below... It is working fine... It
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
alert($('#chkImages').val());
if ($('#chkImages').val() == "1") {
$("#NewsTitle").css('display', '');
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="chkImages" runat="server" Value="1" />
<div id="main" runat="server" style="display: none;">
Test
</div>
<a id="NewsTitle" runat="server" style="display: none" class="specialNews-label">AAAA</a>
</div>
</form>
</body>
Upvotes: 1
Reputation: 62
hey brother there is letter case problem
try this-----
ClientID instead of ClientId
Upvotes: 0