Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing form objects in a function

Status
Not open for further replies.

jgroove

Programmer
Jul 9, 2001
43
0
0
US
I am not very good with javascript but I have kludged some javascript togethter to make a function that works well, but I need to make it more general by being able to pass it the form elements so that I don't have to write ten seperate functions of the same code. I think that there is a way to pass the form objects but am having a difficult time making it work. Here is what I have :

Code:
<script language=&quot;JavaScript&quot;>
<!--
    function ChangeCharacter() {
		MAX_LENGTH = 2000;
		document.frmStep3.txtRemaining.value= 2000 - document.frmStep3.txtLocationDirections.lenght;
		if ( ( document.frmStep3.txtLocationDirections.value  == &quot;&quot; ) || ( document.frmStep3.txtLocationDirections.value.length == 0 )  ) {
        	remainingCharacters = MAX_LENGTH;
        	document.frmStep3.txtRemaining.value = remainingCharacters;
      	} else {
        	remainingCharacters  = MAX_LENGTH - (document.frmStep3.txtLocationDirections.value.length);
        	document.frmStep3.txtRemaining.value = remainingCharacters;
      	}
      	if ( document.frmStep3.txtLocationDirections.value < (MAX_LENGTH + 1) ) {
        	document.frmStep3.txtRemaining.value = remainingCharacters;
      	}
      	if ( ( document.frmStep3.txtRemaining.value < 0 ) ) {
        	document.frmStep3.txtLocationDirections.value = document.frmStep3.txtLocationDirections.value.substring(0,MAX_LENGTH);
        	document.frmStep3.txtRemaining.value = 0;
        	alert(&quot;Maximum message length reached. Your message has been truncated at &quot; + MAX_LENGTH + &quot;  characters.&quot;);
      	}
    }
//-->
</script>



then in the HTML

<textarea name=&quot;txtLocationDirections&quot; cols=&quot;60&quot; rows=&quot;5&quot; wrap=&quot;virtual&quot; onKeyUp=&quot;ChangeCharacter()&quot; onKeyDown=&quot;ChangeCharacter()&quot; onBlur =&quot;ChangeCharacter&quot;></textarea>
<input type=&quot;text&quot; name=&quot;txtRemaining&quot; maxlength=&quot;4&quot; value=&quot;2000&quot; size=&quot;4&quot;>


this code essentially counts down the number of characters that is in a textarea
 
I have another option for you, much shorter and easier:

function countChars(obj) {

return obj.value.length;

}

Then you should call this function from html code with an argument = some form object.
For example:

<input type=&quot;button&quot; name=&quot;&quot; value=&quot;count all chars in some object&quot;
onClick=&quot;alert(countChars(document.formName.txtLocationDirections))&quot;>

good luck
 
sooo....if I want to use the function that I created... I could do something like this???

Code:
<script language=&quot;JavaScript&quot;>
<!--
    function ChangeCharacter(textareaobj,remainingcharobj) {
		MAX_LENGTH = 2000;
		remainingcharobj.value= 2000 - textareaobj.lenght;
		if ( ( textareaobj.value  == &quot;&quot; ) || ( textareaobj.value.length == 0 )  ) {
        	remainingCharacters = MAX_LENGTH;
        	remainingcharobj.value = remainingCharacters;
      	} else {
        	remainingCharacters  = MAX_LENGTH - (textareaobj.value.length);
        	remainingcharobj.value = remainingCharacters;
      	}
      	if ( textareaobj.value < (MAX_LENGTH + 1) ) {
        	remainingcharobj.value = remainingCharacters;
      	}
      	if ( ( remainingcharobj.value < 0 ) ) {
        	textareaobj.value = textareaobj.value.substring(0,MAX_LENGTH);
        	remainingcharobj.value = 0;
        	alert(&quot;Maximum message length reached. Your message has been truncated at &quot; + MAX_LENGTH + &quot;  characters.&quot;);
      	}
    }
//-->
</script>


but is there a simpler way to make the function call than this

onKeyUp=&quot;ChangeCharacter(document.frmStep3.txtLocationDirections,document.frmStep3.txtRemaining)&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top