Choosing visible form elements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anna

    #1

    Choosing visible form elements

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

    var elems = document.formNa me.elements;
    for(i = 0; i < elems.length; i++) {
    if(elems[i].type && elems[i].type.toLowerCa se()=="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='invi sible'?

    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
  • Vjekoslav Begovic

    #2
    Re: Choosing visible form elements

    "Anna" <anna@ubaccess. com> wrote:
    [color=blue]
    > 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='invi sible'?[/color]

    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=n ew Array();
    var divs=myForm.get ElementsByTagNa me("DIV");
    for (var i=0; i<divs.length; i++){
    if (divs.item(i).c lassName == "invisible" )){
    invisibleDivs.p ush(divs.item(i ));
    }
    }
    var textBoxes = myForm.getEleme ntsByTagName("I NPUT");
    for (var i=0; i<textBoxes.len gth; i++){
    var currentTxtBox = textBoxes.item( i);
    if (currentTxtBox. type == "text"){
    var good=true;
    for (var j=0;j<invisible Divs.length;j++ ){
    if (contains(invis ibleDivs.item(j ),currentTextBo x)){
    good=false;brea k;
    }
    }
    if (good){
    //your code here
    }
    }
    }


    Not tested.
    Good luck,

    Vjekoslav


    Comment

    Working...