Choosing visible form elements

Discussion in 'Javascript' started by Anna, Nov 18, 2003.

  1. Anna

    Anna Guest

    Hi all.
    I have a javascript function that loops throught all text boxes inside
    a form:

    var elems = document.formName.elements;
    for(i = 0; i < elems.length; i++) {
    if(elems.type && elems.type.toLowerCase()=="text")
    do something
    }

    This way it goes through every single text box in the form.
    But in my form structure, some form elements (text boxes included)
    are inside invisible divs.
    And what I actually want to do, is to check only the text boxes
    that are not inside invisible divs.
    Is there a way to say in javascript something like:
    choose only textbox that is not inside element (div) whose
    className='invisible'?

    Or maybe I should try a different approach, instead looping through
    all form elements?

    Thank you very much for your help.
    Sorry if I'm asking stupid questions, I'm not very good with
    javascript yet.

    Anna
    Anna, Nov 18, 2003
    #1
    1. Advertising

  2. "Anna" <> wrote:

    > And what I actually want to do, is to check only the text boxes
    > that are not inside invisible divs.
    > Is there a way to say in javascript something like:
    > choose only textbox that is not inside element (div) whose
    > className='invisible'?


    function contains(parent,child){
    if (parent==child) return true;
    var t = child;
    if (t && t.parentNode){
    if (t.parentNode == parent)
    return true;
    else
    return contains(parent,t.parentNode)
    }
    return false;
    }

    var myForm=document.getElementById("your_form_id");
    var invisibleDivs=new Array();
    var divs=myForm.getElementsByTagName("DIV");
    for (var i=0; i<divs.length; i++){
    if (divs.item(i).className == "invisible")){
    invisibleDivs.push(divs.item(i));
    }
    }
    var textBoxes = myForm.getElementsByTagName("INPUT");
    for (var i=0; i<textBoxes.length; i++){
    var currentTxtBox = textBoxes.item(i);
    if (currentTxtBox.type == "text"){
    var good=true;
    for (var j=0;j<invisibleDivs.length;j++){
    if (contains(invisibleDivs.item(j),currentTextBox)){
    good=false;break;
    }
    }
    if (good){
    //your code here
    }
    }
    }


    Not tested.
    Good luck,

    Vjekoslav
    Vjekoslav Begovic, Nov 18, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. spamfurnace
    Replies:
    1
    Views:
    2,632
    Scott M.
    May 24, 2004
  2. tshad

    button visible/not visible

    tshad, Oct 26, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,444
    Brian Hoops
    Oct 28, 2004
  3. tshad

    Sections visible and not visible

    tshad, Jan 31, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,059
    tshad
    Jan 31, 2005
  4. rjweytens
    Replies:
    0
    Views:
    371
    rjweytens
    Jun 8, 2004
  5. Mr. Smith
    Replies:
    2
    Views:
    106
    Mr. Smith
    Oct 20, 2004
Loading...

Share This Page