Jquery script not working after postback even with pageLoad
I am having problems with a JQuery script on my Asp.net page. During the first boot it works well, after the postback it doesn't. Initiali I thought it was because the script no longer loads, but alert told me the script was working as I am using the pageLoad () function instead of $ (document) .ready (function (). In the script I just read the value from one textbox and set the length value for the next text input. I put the script on the page like this:
<script type = "text / javascript" src = "../js/smsanywhere.js"> </script>
I tried to place directly on page, but I don't think that's not the case.
The script is shown below.
function pageLoad()
{
alert('pageLoad is there');
var textBox = "textarea[id*='txtMessage']";
var counterBox = "input[id*='txtCharLeft']";
$(textBox).keydown(function(){
alert(this);
var length = 70 - $(this).val().length;
$(counterBox).val(length);
$(counterBox).attr("style",function(){
return "width:30px;" + (length < 0 ? "background-color:Red;" : "");
});
});
}
I am not using any ajax on this page, its only the master page, content page and jquery.
What I am missing ...
source to share
I found the problem .... as I am adding a link to jquery like this:
HtmlGenericControl myJs = new HtmlGenericControl();
myJs.TagName = "script";
myJs.Attributes.Add("type", "text/javascript");
myJs.Attributes.Add("language", "javascript"); //don't need it usually but for cross browser.
myJs.Attributes.Add("src", ResolveUrl("~/js/jquery/jquery-1.2.6.min.js"));
Page.Header.Controls.Add(myJs);
But I put inside if (! IsPostBack) {...}, so I was ... a solution just to remove it from this condition and run it every time ... uff.
So, the mystery has been sorted out ...
source to share