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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing the control or control Name to a function 1

Status
Not open for further replies.

Deadline

Programmer
Feb 28, 2001
367
US
Hi,
I actually need to perform the same task on a set of Checkboxes and TextAreas. Basically, I have 5 text boxes. I want to call a function each time the user changes something in the textbox.

When the textarea is empty, the checkbox gets cleared. Similarly, when the user types something in the textarea, the checkbox should get checked automatically.

I thought instead of having 5 different functions to do this task, why not just one that'll accept the control names (checkboxname, textareaname) and perform the task.

But it is a real hell to figure out a way to make it work.

Can you help me in this or can you suggest some better way to do this ?

ClientSide VBSCRIPT is OK.


Thanks in advance.


RR
:-(
 
Here's some client-side VB script and the html for one of the controls. I have several routines that I use to handle the same types of things for similar controls so I don't have to code the same stuff over and over again. You should be able to get an idea how to do what YOU want to do from this. P.S. not sure if this all works in IE4, but it does in 5:

'**************************************************************
'**************************************************************
Sub HandleDateKeyPress(ByRef objElement)

Const DELETE_KEY = 46

If Window.Event.Keycode = DELETE_KEY Then
objElement.value = ""
Window.Event.Keycode = 0
Else
If (objElement.ID = "txtDateClosed" Or objElement.ID = "txtDatePaid") And window.frmOrder.txtLiability.value = "" Then
Window.Event.Keycode = 0
MsgBox "You must enter the amount of liability before you can enter this date.",vbExclamation,"Please Enter Liability Amount"
Else
ProcessDateKeyPress objElement, window.event.keyCode
End If
End If

End Sub

Code:
<INPUT	type=&quot;text&quot; ID=&quot;txtDateEntered&quot; name=&quot;txtDateEntered&quot; class=&quot;InputBox&quot; DateRangeCode=&quot;0&quot; tabindex=&quot;28&quot;
	style=&quot;position:absolute;top:5;left:120;width:60;&quot;
	onKeyDown=&quot;HandleKeyDown me, False&quot; OnKeyPress=&quot;HandleDateKeyPress me&quot; OnFocus=&quot;HandleDateFocus me&quot; OnBlur=&quot;HandleDateBlur me&quot;
	value=&quot;&quot;>
 
If you're willing to change the name property of your input elements and are OK with JavaScript, I can help. For example, you're HTML could look like this.

Code:
<input type=&quot;text&quot; name=&quot;1textfield&quot; value=&quot;&quot; size=&quot;10&quot;>
<input type=&quot;checkbox&quot; name=&quot;1checkbox&quot; onChange=&quot;return validate(this)&quot;;>Goes with TextBox1</input>

<input type=&quot;text&quot; name=&quot;2textfield&quot; value=&quot;&quot; size=&quot;10&quot;>
<input type=&quot;checkbox&quot; name=&quot;2checkbox&quot; onClick=&quot;return validate(this)&quot;;>Goes with TextBox2</input>

Essentially you could write one Javascript function that all of your elements could access. Passing the &quot;this&quot; paramater to the function gives the function a reference to that element. In the HTML design above, I made sure that the word &quot;text&quot; is in all text field names and the word &quot;check&quot; is in all checkbox names. I then placed a numeric identifier as the first character of each input element name that links the textfield with the checkbox. Then when the Javascript function is called, you could examine the name property of the calling element to determine if it is a checkbox or textfield calling the function. The function structure would look like this.

Code:
function validate(form_elm) {
     if (calling element is a text box) {
          code to manipulate checkbox state
     } else {
          code to manipulate text box value or checkbox
          state or both
     }
}

I would then write code to grab the first character of the name property then loop through all of the form elements looking for another element with that same value as the first character. When the code found it, it would need to do one more examination to make sure it hasn't found the calling element, then manipulate the value or checked state at that point. After manipulation is done, a return statement would exit the function.

If you're interested, let me know and I'll give you a code example.

TW
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top