...

View Full Version : Assistance with a global variable.. (?)



Smartmonkey
11-12-2004, 09:25 PM
Ok I think my issue here is that this is a local variable and not a global variable, but I am trying to take this code and make it so it will count text in multiple text fields, instead of one:




<html>

<head>
<title>New Page 1</title>

<script language = "Javascript">

function taLimit() {
var taObj=event.srcElement;
if (taObj.value.length==taObj.maxLength*1) return false;
}


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.maxLength-taObj.value.length;
}
</script>
</head>

<body>

<label for="ITCon">IT Contact Info: </label>
<input type="text" name="ITCon" title="IT Contact Info: " onkeypress="return taLimit()" onkeyup="return taCount(myCounter)" maxLength="348" >

<Label for="description">Problem Description: </label>
<TEXTAREA onkeypress="return taLimit()" onkeyup="return taCount(myCounter)"
name=Description wrap=physical rows=4 cols=40 maxLength="348"></TEXTAREA>
<br><br>
You have <B><SPAN id=myCounter>348</SPAN></B> characters remaining
for your description...
</body>

</html>


If you see here the problem is, it will count text from each field seperately.

I don't know if I was going about this right or not but I was going to try and declare the variable at the beginning of the code by changing it to this:




<script language = "Javascript">

var taObj=event.srcElement;

function taLimit() {

if (taObj.value.length==taObj.maxLength*1) return false;
}


function taCount(visCnt) {

if (taObj.value.length>taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1);
if (visCnt) visCnt.innerText=taObj.maxLength-taObj.value.length;
}
</script>


but of course when I run that I get an error saying taObj is null or not an object.

Sorry if I am way off base here, but im a newb.. just trying to piece my way through this. If there is a better way to count all the text im all ears.. but I really do appreciate any help that can be offered =)

Roy Sinclair
11-12-2004, 10:21 PM
Try:



<script language = "Javascript">
var taObj;
function taLimit() {
taObj=event.srcElement;
if (taObj.value.length==taObj.maxLength*1) return false;
}


function taCount(visCnt) {
taObj=event.srcElement;
if (taObj.value.length>taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1);
if (visCnt) visCnt.innerText=taObj.maxLength-taObj.value.length;
}
</script>


Define the variable globally but since it's value is assigned when the event occurs you can't make that assignment at the global level, you can only make the assignment when the event occurs.

Smartmonkey
11-13-2004, 12:57 AM
Hey, I appreciate the response..

I tried what you suggested and I didn't get any errors this time, however, it still did the same thing even though it was a global variable. for instance if you type in field 1 it will start counting down from 348, then if you go to field 2, it will start counting down from 348 again.. instead of continuing from the new number that field 1 generated...

I hope what im doing make sense...

thanks for the quick response! :D

Smartmonkey
11-14-2004, 06:27 PM
:eek: ...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum