Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Assistance with a global variable.. (?)

    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:

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

    Code:
    <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 =)

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try:

    Code:
    <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.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #3
    New Coder
    Join Date
    Oct 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    !

    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!

  • #4
    New Coder
    Join Date
    Oct 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •