I have a hidden field on my aspx page that I want to use to store the href value of the currently selected tab (using bootstrap tabs). One of the tabs has some search functionality and when the postback happens from the search, the page loads the default active tab. I've tried finding many solutions for this, but none seem to work. This solution looked promising, but the hidden field wasn't in the Request.Form.
So, I started messing around with my own solution, where when a tab is clicked, set the value of the hidden field to the href attribute of the clicked tab, and when the button is clicked, store the value in the session. My problem is this: I can't for the life of me find a way to get/set the value of the hiddenfield. After reading many similar issues people had here on stack, I'm starting to wonder if I'm trying to get/set the value when it isn't in the DOM.
I know I'm good at first using $(document).ready() but I'm not sure what happens when within that I have a click event like $(myTab a).click(). Within this event, is it possible I'm trying to get elements that are no longer in the DOM? I have tried many many many different ways, and I keep getting null or undefined when trying to do this. To end this rant/question, I'll post my code with all the different ways I've been trying to do this.
<div id="maintabs" runat="server">
<ul class="nav nav-tabs" id="myTab">
<li class="active"><a href="#home" data-toggle="tab">Main</a></li>
<li><a href="#Activity" data-toggle="tab">System Notes</a></li>
<li><a href="#Guidelines" data-toggle="tab"><%= SBL.GetConsiderations(CompanyID) %></a></li>
<li><a href="#Steps" data-toggle="tab">System Steps</a></li>
<li><a href="#Accepted" data-toggle="tab">Employees Accepted</a></li>
<li><a href="#Files" data-toggle="tab">Locate A File</a></li>
</ul>
<div class="tab-content">
.
.
.
<div class="hidWrapper">
<asp:HiddenField ID="hidLastTab" Value="0" runat="server" />
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#myTab a").click(function() {
// This is basically what I'm trying to accomplish
$("#hidLastTab").val($(this).attr("href").replace("#", ""));
// So first test...
console.log($(this).attr("href").replace("#", "")) // Console logged Files as expected, so this is good
// My next test was then this
console.log($("#hidLastTab").val()); // Console logged undefined
// Next attempts
console.log($("#hidLastTab").value); // still undefined
var myHidden = document.getElementById('<%= hidLastTab.ClientID %>');
console.log(myHidden.value); // Cannot read property 'value' of null
var myHidden = document.getElementById('<%= hidLastTab.ClientID %>')
console.log(document.getElementById(myHidden)); // null
console.log(document.getElementById(myHidden).value); // Cannot read property 'value' of null
var myHidden = '<%= hidLastTab.ClientID %>'; // ctl00_ContentPlaceHolder1_hidLastTab
console.log(myHidden); // null
console.log(document.getElementById(myHidden)); // Cannot read property 'value' of null
console.log(document.getElementById(myHidden).value); // Cannot read property 'val' of null
// per one recommendation, a div wrapper
console.log($(".hidWrapper input[type=hidden]").val()); // undefined
// Another recommendation
console.log($("[id$=_hidLastTab").attr['value']); // undefined
})
})
<script>
Please if anyone can help me out here that would be sooooo amazing, as this is driving me crazy! I even tried doing the same thing with a textbox since at least I know I should be able to getElementById for that (I don't really know how hidden fields work, and since I can't find it in the markup, I wasn't 100% sure getElementById would work like other elements), and even the textbox was returning null when doing getElementById("txtNotes") AND getElementById('<%= txtNotes.ClientID %>'). This is what lead me to believe that maybe it is a problem with the elements not existing with the DOM at that time.
Thanks so much in advance! Andy