srcElement and innerText and?

G

GarryJones

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

Martin Honnen

GarryJones said:
<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-HTML/html.html#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")
 
N

Nick S

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

Kiran Makam

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></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>
 
S

SAM

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>
 
N

Nick S

GarryJones a écrit :


<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>

Yeah, Sam's is better... use his :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top