SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Egypt
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile check boxes and text boxes

    I have a form in which there are check boxes, in front of each check box there is text box,

    Function Required:

    I want that if a user checks a check box, the mouse stands on its crossponding text box and set its value to 1.

    Question:

    I do not know how many check boxes and text boxes will be on the form since I retrive information displayed in the form from a db and the number of the check boxes and the text boxes is based on the records of the db.

    I have made a javascript that performs the function explained above but the problem is that when the user checks a check box, the mouse does not stand on its crossponding text box (the text box infront of the checked check box), but it stands in another one.

    can some body help me fixing this in the javascript

    <form name = 'theForm' id = 'theForm'>
    <Table Width = "60%" Border = 1>
    <%for i = 1 to 5 %>
    <Tr>
    <Td Height = "50" VAlign = "Top" Class = "OutLined">
    <Input Type = "CheckBox" Name = "Chk_Insert" Value = "<%=I%>" onClick="cycleCheckboxes(this.form)" >
    </Td>
    <Td VAlign = "Top" Class = "OutLined2">
    <Input Type = "text" Name = "Txt_Qty" > <%=I%>
    </Td>
    </Tr>

    <%next%>
    </form>

    <SCRIPT LANGUAGE="JavaScript"><!--
    function cycleCheckboxes(what) {
    for (var i = 0; i<what.elements.length; i++) {
    if ((what.elements[i].name.indexOf('Chk_Insert') > -1)) {
    if (what.elements[i].checked) {

    what.elements['Txt_Qty'][i].value = i;
    what.elements['Txt_Qty'][i].focus();

    }
    else {
    what.elements['Txt_Qty'][i].value = '';
    }

    }
    }
    }
    //--></SCRIPT>

  2. #2
    SitePoint Addict
    Join Date
    Jul 2000
    Posts
    201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The 'elements' property return all elements, so I think you would need to use 'getElementsByName' instead.
    Code:
    function cycleCheckboxes(what) {
      for (var i = 0; i<document.getElementsByName('Chk_Insert').length; i++) {
        if (document.getElementsByName('Chk_Insert')[i].checked) {
          document.getElementsByName('Txt_Qty')[i].value = i+1;
          document.getElementsByName('Txt_Qty')[i].focus();
        } else {
          document.getElementsByName('Txt_Qty')[i].value = '';
        }
      }
    }

  3. #3
    SitePoint Addict
    Join Date
    Jul 2000
    Posts
    201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oops I just noticed that focus doesn't work right if more than one box is checked. Try this:
    Code:
    function cycleCheckboxes(what,i) {
          if (what.elements[(i-1)*2].checked) {
            what.elements[(i-1)*2+1].value = i;
            what.elements[(i-1)*2+1].focus();
          } else {
            what.elements[(i-1)*2+1].value = '';
          }
    }
    In the form, add this to the checkbox
    Code:
    <Input Type = "CheckBox" Name = "Chk_Insert" Value = "<%=I%>" onClick="cycleCheckboxes(this.form,<%=I%>)" >

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Egypt
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Thanks

    Hi FauxPas,
    I know I am very late to send a reply for your response to my problem but you deserve a 'thank you' and I had never forgotten this but I was so busy but a VERY BIG THANK to YOU for your help. Your code has helped me much.
    Thanks again
    Rasha zaki


Bookmarks

Posting Permissions

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