Connecting Tech Pros Worldwide Help | Site Map

srcElement and innerText and?

GarryJones
Guest
 
Posts: n/a
#1: Sep 6 '08
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.maxLengt h*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.maxLengt h*1);
if (visCnt) visCnt.innerHTML=taObj.maxLength-taObj.value.length;
}

</script>

You have <B>
<div id="txtmsg" class="scfmfrm_dept">&nbsp;</div>

<span id="myCounter">255</SPAN></Bcharacters 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
Guest
 
Posts: n/a
#2: Sep 6 '08

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
http://www.w3.org/TR/html4/interact/forms.html#h-17.7
Consequently the DOM does not define a maxLength property, see
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-24874179
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/
Nick S
Guest
 
Posts: n/a
#3: Sep 6 '08

re: srcElement and innerText and?


On Sep 6, 11:30*am, GarryJones <mor...@algonet.sewrote:
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.maxLengt h*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.maxLengt h*1);
*if (visCnt) visCnt.innerHTML=taObj.maxLength-taObj.value.length;
>
}
>
</script>
>
You have <B>
<div id="txtmsg" class="scfmfrm_dept">&nbsp;</div>
>
<span id="myCounter">255</SPAN></Bcharacters 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></bcharacters
remaining<br>
<textarea id="ta" onkeyup="showLimit('ta', 10, 'numLeft');"></
textarea>
</body>
</html>
Kiran Makam
Guest
 
Posts: n/a
#4: Sep 6 '08

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">&nbsp;</div>

<span id="myCounter">255</SPAN></Bcharacters 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



SAM
Guest
 
Posts: n/a
#5: Sep 6 '08

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
Guest
 
Posts: n/a
#6: Sep 6 '08

re: srcElement and innerText and?


On Sep 6, 1:58*pm, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
Quote:
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
Yeah, Sam's is better... use his :)
Closed Thread