I have two separate jquery popup, one of which is populated from a GridView
data:
//THIS IS THE POPUP WHICH PRE POPULATES THE TEXTAREA VALUE FROM THE GRIDVIEW
<asp:UpdatePanel runat="server" ID="upPop" UpdateMode="Conditional">
<ContentTemplate>
<div id="popupContactEM">
<a id="popupContactCloseEM" title="Close Window">x</a>
<div id="dvFourthEM" class="mainFirst">
<div id="leftdiv1EM" class="leftdivspec"><sup style="color: #FF0000; font-weight: bold;">*</sup>Message:<br /><br /><b style="font-size: xx-small;">Character Count: </b><input maxlength="4" size="4" id="charCountE" style="border: none; font-size: xx-small; color: #E55302;" disabled="disabled" /><br /><b style="font-size: xx-small;">Word Count: </b><input maxlength="4" size="4" style="border: none; font-size: xx-small; color: #E55302;" id="wordCountE" disabled="disabled" /></div>
<div id="rightdiv1EM" class="rightdivspec"><asp:TextBox ID="tbMessageEM" ClientIDMode="Static" runat="server" TextMode="MultiLine" Columns="30" Rows="5"></asp:TextBox></div>
</div>
</div>
<div id="backgroundPopupEM"></div>
</ContentTemplate>
</asp:UpdatePanel>
<div id="popupContact">
<a id="popupContactClose" title="Close Window">x</a>
<div id="dvFourth" class="mainFirst">
<div id="leftdiv1" class="leftdivspec"><sup style="color: #FF0000; font-weight: bold;">*</sup>Message:<br /><br /><b style="font-size: xx-small;">Character Count: </b><input maxlength="4" size="4" id="charCount" style="border: none; font-size: xx-small; color: #E55302;" disabled="disabled" /><br /><b style="font-size: xx-small;">Word Count: </b><input maxlength="4" size="4" style="border: none; font-size: xx-small; color: #E55302;" id="wordCount" disabled="disabled" /></div>
<div id="rightdiv1" class="rightdivspec"><asp:TextBox ID="tbMessage" ClientIDMode="Static" runat="server" TextMode="MultiLine" Columns="30" Rows="5"></asp:TextBox></div>
</div>
</div>
<div id="backgroundPopup"></div>
I am trying to display the WORD and CHARACTER count for either popup with and I have the following JQuery:
$(document).ready(function () {
$("#tbMessage").keyup(function () {
WordCounter(this, 0);
});
$("body").on('keyup', "#tbMessageEM", function (e) {
WordCounter(this, 1);
});
});
function WordCounter(txt, whc) {
if (whc == 0) {
s = txt.value;
}
else {
s = txt;
}
if (s.length == 0) {
len = 0;
}
else {
m1 = s.replace(/\s/g, '+');
m = m1.replace(/^\s/g, '+');
len = countWords(m);
}
if (whc == 0) {
document.getElementById("charCount").value = s.length;
document.getElementById("wordCount").value = len;
}
else {
document.getElementById("charCountE").value = s.length;
document.getElementById("wordCountE").value = len;
}
}
function countWords(str) {
str1 = str.replace(/\+*$/gi, "");
str2 = str1.replace(/\++/g, ' ');
var temp = new Array();
temp = str2.split(' ');
return temp.length;
}
When the popup which is not pre-populated opens, I am able to enter the text inside the TextArea
and it gives me the count.
When the popup which is prepopulated opens, it does give the word/character count from the text inside the TextArea
but when I go to enter or delete anything, I get the following error: 0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'replace'
:
Please help me resolve the error.
txt
was coming up [object HTML TextAreaElement]
.
I changed the code to this:
if (whc == 0) {
s = txt.value;
}
else {
s = $("#tbMessageEM").val();;
}
Instead of getting txt
, I am getting the value of the textarea and it is working fine.
Why it wasn't working:
For the pre-populated textarea, I was loading the popup this way:
function loadPopupEM() {
//loads popup only if it is disabled
if (popupStatusEM == 0) {
$("#backgroundPopupEM").css({
"opacity": "0.7"
});
$("#backgroundPopupEM").fadeIn("slow");
$("#popupContactEM").fadeIn("slow");
popupStatusEM = 1;
var w = $("#tbMessageEM").val(); //get the prepopulated text
WordCounter(w, 1);
}
}
When the popup loaded, I got the following error in the line if (s.length == 0)
:
0x800a138f - JavaScript runtime error: Unable to get property 'length' of undefined or null reference