|  | srcElement and innerText and? |  | |
| | | GarryJones |  |
| Posted: Sat Sep 06, 2008 9:30 am Post subject: srcElement and innerText and? |  |
| |  | |
To show users how many characters they have left in a TEXTAREA input I have been using "taCount" from a website I googled.
function taCount(visCnt) { var taObj=event.srcElement; if (taObj.value.length>taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1); if (visCnt) visCnt.innerText=taObj.value.length; }
After googling I found out that firefox does not allow "event.srcElement" and "innerText" to work so I tried substituting with
var taObj = (event.target) ? event.target : event.srcElement innerText to innerHTML
But it is still not working.
Entire code..
<script language="JavaScript" type="text/javascript">
function taLimit(taObj) { var taObj = (event.target) ? event.target : event.srcElement if (taObj.value.length==taObj.maxLength*1) return false; }
function taCount(taObj, visCnt) { var taObj = (event.target) ? event.target : event.srcElement if (taObj.value.length>taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1); if (visCnt) visCnt.innerHTML=taObj.maxLength-taObj.value.length; }
</script>
You have <B> <div id="txtmsg" class="scfmfrm_dept"> </div>
<span id="myCounter">255</SPAN></B> characters remaining for your description..
<TEXTAREA onkeypress="taLimit(this)" onkeyup="return taCount(this, myCounter)" name=Description rows=7 wrap=physical cols=40 maxLength="255"></ TEXTAREA>
Anyone know what other problems firefox is having with this?
Garry Jones Sweden |
| |
| | | Martin Honnen |  |
| Posted: Sat Sep 06, 2008 9:33 am Post subject: Re: srcElement and innerText and? |  |
GarryJones wrote:
| Quote: | TEXTAREA onkeypress="taLimit(this)" onkeyup="return taCount(this, myCounter)" name=Description rows=7 wrap=physical cols=40 maxLength="255"></ TEXTAREA
Anyone know what other problems firefox is having with this?
|
There is no maxlength attribute defined for 'textarea' elements. see LINK Consequently the DOM does not define a maxLength property, see LINK That way your code taObj.maxLength gives undefined with Firefox. Try taObj.getAttribute("maxlength") instead or (for IE I fear) taObj.getAttribute("maxLength")
--
Martin Honnen http://JavaScript.FAQTs.com/ |
| |
| | | SAM |  |
| Posted: Sat Sep 06, 2008 9:58 am Post subject: Re: srcElement and innerText and? |  |
GarryJones a écrit :
| Quote: | To show users how many characters they have left in a TEXTAREA input I have been using "taCount" from a website I googled.
|
<script type="text/javascript"> function leftText(where, max) { if(!where.repport) { where.repport = document.createElement('P'); where.parentNode.insertBefore(where.repport, where); } where.repport.className = ''; var t = where.value; var q = max - t.length; where.repport.innerHTML = q>0? 'Characters remaining : '+q : q==0? 'Filled up !' : 'Filling back'; if(q<0) { where.value = t.substring(0,t.length-1); where.repport.className = 'red'; } }
</script>
<style type="text/css"> ..red { color: red; font-weight: bold } </style>
<form> <textarea onkeyup="leftText(this, 25);">test</textarea> <textarea onkeyup="leftText(this, 15);">test</textarea> </form>
-- sm |
| |
| | | Nick S |  |
| Posted: Sat Sep 06, 2008 11:44 am Post subject: Re: srcElement and innerText and? |  |
| |  | |
On Sep 6, 11:30 am, GarryJones <mor...@algonet.se> wrote:
| Quote: | To show users how many characters they have left in a TEXTAREA input I have been using "taCount" from a website I googled.
function taCount(visCnt) { var taObj=event.srcElement; if (taObj.value.length>taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1); if (visCnt) visCnt.innerText=taObj.value.length;
}
After googling I found out that firefox does not allow "event.srcElement" and "innerText" to work so I tried substituting with
var taObj = (event.target) ? event.target : event.srcElement innerText to innerHTML
But it is still not working.
Entire code..
script language="JavaScript" type="text/javascript"
function taLimit(taObj) { var taObj = (event.target) ? event.target : event.srcElement if (taObj.value.length==taObj.maxLength*1) return false;
}
function taCount(taObj, visCnt) { var taObj = (event.target) ? event.target : event.srcElement if (taObj.value.length>taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1); if (visCnt) visCnt.innerHTML=taObj.maxLength-taObj.value.length;
}
/script
You have <B div id="txtmsg" class="scfmfrm_dept"> </div
span id="myCounter">255</SPAN></B> characters remaining for your description..
TEXTAREA onkeypress="taLimit(this)" onkeyup="return taCount(this, myCounter)" name=Description rows=7 wrap=physical cols=40 maxLength="255"></ TEXTAREA
Anyone know what other problems firefox is having with this?
Garry Jones Sweden
|
Give this a crack, it's only simple but it works in IE, FF and chrome.
<html> <head> <script> function showLimit(src, limit, displayElem) { var srcElem = document.getElementById(src); var displayElem document.getElementById(displayElem); var strText= srcElem.value; var intLen = strText.length; var intRemaining = limit - intLen;
if (intRemaining <= 0) { var newVal = srcElem.value.substr(0, limit); srcElem.value = newVal; displayElem.innerHTML = 0; return; } displayElem.innerHTML = intRemaining; } </script> </head> <body> You have <b><span id="numLeft">10</span></b> characters remaining<br> <textarea id="ta" onkeyup="showLimit('ta', 10, 'numLeft');"></ textarea> </body> </html> |
| |
| | | Kiran Makam |  |
| Posted: Sat Sep 06, 2008 11:45 am Post subject: Re: srcElement and innerText and? |  |
| |  | |
Garry,
- In IE, event is a global object. In firefox, you have to get it through the event handler. Firefox passes event object as the first param to the event handler. - maxLength attribute is valid for INPUT fields, but not valid for TEXTAREA - To access 'myCounter' span, get the reference using document.getElementById
I have made the above mentioned changes, this is the modified code: -------- <script language="JavaScript" type="text/javascript">
function taLimit(event, taObj, maxLength) { var taObj = (event.target) ? event.target : event.srcElement if (taObj.value.length==maxLength) return false; }
function taCount(event, taObj, maxLength, visCnt) { var taObj = (event.target) ? event.target : event.srcElement
if (taObj.value.length>taObj.maxLength) taObj.value=taObj.value.substring(0,maxLength);
if (visCnt) visCnt.innerHTML=maxLength-taObj.value.length; }
</script>
You have <B> <div id="txtmsg" class="scfmfrm_dept"> </div>
<span id="myCounter">255</SPAN></B> characters remaining for your description..
<TEXTAREA onkeypress="taLimit(event, this, 255)" onkeyup="return taCount(event, this, 255, document.getElementById('myCounter'))" name=Description rows=7 wrap=physical cols=40></TEXTAREA> --------
Kiran Makam |
| |
| | | Nick S |  |
| Posted: Sat Sep 06, 2008 12:06 pm Post subject: Re: srcElement and innerText and? |  |
On Sep 6, 1:58 pm, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid> wrote:
| Quote: | GarryJones a écrit :
To show users how many characters they have left in a TEXTAREA input I have been using "taCount" from a website I googled.
script type="text/javascript" function leftText(where, max) { if(!where.repport) { where.repport = document.createElement('P'); where.parentNode.insertBefore(where.repport, where); } where.repport.className = ''; var t = where.value; var q = max - t.length; where.repport.innerHTML = q>0? 'Characters remaining : '+q : q==0? 'Filled up !' : 'Filling back'; if(q<0) { where.value = t.substring(0,t.length-1); where.repport.className = 'red'; }
}
/script
style type="text/css" .red { color: red; font-weight: bold } /style
form textarea onkeyup="leftText(this, 25);">test</textarea textarea onkeyup="leftText(this, 15);">test</textarea /form
-- sm
|
Yeah, Sam's is better... use his  |
| |
|
|