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!

Checkbox with associated textfields

Status
Not open for further replies.

mike10101

Vendor
May 6, 2005
13
GB
Hi,

I have been struggling to work out the best way of associating a group of dynamically generated checkboxes with their corresponding textfields. Do you know where I can find any good examples of this?

Thanks, Mike
 
>I have been struggling to work out the best way of associating a group of dynamically generated checkboxes with their corresponding textfields.
So what is the second best you seem to have?

>Do you know where I can find any good examples of this?
I only have my example if you don't mind. But since you ask in vbs forum, I can offer you a vbs example. Don't complaint it works only in ie kind of triviality people seem to derive their special kind of pleasure.
[tt]
<html>
<head>
<script language="vbscript">
sub setup
dim oform, oelem, i
set oform=document.formname
for i=0 to 3
set oelem=document.createElement("input")
with oelem
.type="checkbox"
.value="a" & i
.name="chkbox"
.attachEvent "onclick",getref("handler")
end with
oform.appendChild oelem
set oelem=document.createTextNode("a" & i)
oform.appendChild oelem
set oelem=document.createElement("br")
oform.appendChild oelem
set oelem=nothing
next
set oform=nothing
end sub
sub handler
dim oelem
set oelem=window.event.srcElement
alert(oelem.value)
end sub
set window.onload=getref("setup")
</script>
</head>
<body>
<form name="formname" id="formid">
</form>
</body>
</html>
[/tt]
 
The process can get rather elaborate. The answer also depends on what you mean by "associate" and "textfield."
Code:
<html>
  <!-- saved from url=(0011)about:blank -->
  <head>
    <script language="VBScript">
      'Notes:
      '       <br> elements' id values are not entered into a <form>'s
      '       namespace, but are always global to document.
      '
      '       Element collections are created whenever two elements
      '       with the same id exist within a namespace scope, but as
      '       soon as only one element remains in the collection it is
      '       promoted to an element directly contained within the
      '       parent element.
      '
      '       To tie parallel collections together it is useful to
      '       create an expando property containing the index of each
      '       element via setAttribute.
      
      Option Explicit
      
      Function Exists(ByVal ElementId)
        Exists = Not (document.getElementById(ElementId) Is Nothing)
      End Function
      
      Function IsCollection(ByVal Element)
        IsCollection = TypeName(Element) = "DispHTMLElementCollection"
      End Function

      Sub btnSet_onclick()
        Dim intItem, strChoices, objNew
        
        With frmData
          If Exists("brFrmLine") Then
            If IsCollection(brFrmLine) Then
              For intItem = brFrmLine.length - 1 To 1 Step -1
                .removeChild brFrmLine(intItem)
                .removeChild .txtItem(intItem)
                .removeChild .chkItem(intItem)
              Next
            End If
            .removeChild brFrmLine
            .removeChild .txtItem
            .removeChild .chkItem
          End If
          
          strChoices = Trim(txtChoices.value)
          If Len(strChoices) > 0 Then
            strChoices = Split(strChoices, ",")
            For intItem = 0 To UBound(strChoices)
              Set objNew = document.createElement("input")
              With objNew
                .type = "checkbox"
                .name = "check"
                .id = "chkItem"
                .setAttribute "assoc", intItem
                .attachEvent "onclick", getRef("chkItem_onclick")
              End With
              .appendChild objNew

              Set objNew = document.createElement("input")
              With objNew
                .type = "text"
                .name = "text"
                .id = "txtItem"
                .value = Trim(strChoices(intItem))
              End With
              .appendChild objNew

              Set objNew = document.createElement("br")
              objNew.id = "brFrmLine"
              .appendChild objNew
            Next
          End If
        End With
      End Sub
      
      Sub chkItem_onclick()
        Dim Obj, Color

        Set Obj = window.event.srcElement
        If Obj.checked then
          Color = "pink"
        Else
          Color = "transparent"
        End If
        
        If IsCollection(frmData.txtItem) Then
          frmData.txtItem(Obj.assoc).style.backgroundColor = Color
        Else
          frmData.txtItem.style.backgroundColor = Color
        End If
      End Sub
      
      Sub window_onload()
        Dim intItem
        
        With frmData
          If IsCollection(.chkItem) Then
            For intItem = 0 To .chkItem.length - 1
              With .chkItem(intItem)
                .setAttribute "assoc", intItem
                .attachEvent "onclick", getRef("chkItem_onclick")
              End With
            Next
          End If
        End With
      End Sub
    </script>
  </head>
  <body>
    Enter a list of choices separated by commas:<br>
    <input id="txtChoices" size="60" value="This, That, Another">
    <br>
    <button id="btnSet">Set</button>
    <form name="data" id="frmData">
      <input type="checkbox" name="check" id="chkItem">
      <input type="text" name="text" id="txtItem" value="zero">
      <br id="brFrmLine">
      <input type="checkbox" name="check" id="chkItem">
      <input type="text" name="text" id="txtItem" value="one">
      <br id="brFrmLine">
    </form>
  </body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top