Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12

Thread: HTML Controls

  1. #1
    New Coder
    Join Date
    Oct 2002
    Location
    Bangalore,India
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post HTML Controls

    Hi All,
    I have a HTML in which I can have any Number of Controls like textbox,combobox or radio,checkbox. I want to know the type so that i can take the value accordingly. I am able to get the type for all these controls,but for radio it is showing undefined coz it has to be accessed as arrays.If i know it is radio button then i can access as Array before that how can check whether it is array or not any Suggestions.
    Thanks in Advance.
    Anandraj

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,968
    Thanks
    0
    Thanked 236 Times in 233 Posts
    is it a DOM question?

    anyway, are you doing it like this?

    <script>
    var f = document.forms[0]; //if you have only 1 form
    for (var i=0;i<f.elements.length;i++){
    alert(f.elements[i].name+ ": " +f.elements[i].type)
    }
    </script>

    there should be no problem with radio buttons

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Well...if your accessing the actual element objects (Form.elements[]), using integer indices, .type works fine. On the other hand: if you're referencing the elements by name, as you seemed to be noting, you'll get the array containing references (pointers) to the element objects. You can test for this:

    if (form_ref.element_name.length) {....

    Seems to me I've used .constructor == Array - but it doesn't seem to work now; the above is guaranteed.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,968
    Thanks
    0
    Thanked 236 Times in 233 Posts
    i have also thought of that adios, but if you are accessing elements by its name, you would know what element it is, meaning you would know it's a radio button, unless of course, the radio buttons are generated dynamically and there is a chance that only one radio will exist.

    so i assumed that anandraj is accessing the elements by elements array.

  • #5
    New Coder
    Join Date
    Oct 2002
    Location
    Bangalore,India
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi All,
    Thanks for the reply. Actaully In my HTML i have more Elements, in that some of them are dynamically generated from the server and i am getting the names in a javascript array so i have to go thro' loop constructing obj for every html control and checking for the type ,I am able to get the type for text and other controls but not for Radio coz it has more radio buttons grouped. Its returning undefined if i check for the first radio button.
    Hope now it is clear.

    Thanks and Regards
    anandraj

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,968
    Thanks
    0
    Thanked 236 Times in 233 Posts
    post the code pls

  • #7
    New Coder
    Join Date
    Oct 2002
    Location
    Bangalore,India
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi glen,
    I have given the sniipet below. Just execute the HTML u will get undefined for radio button.

    Thanks and Regards
    Anandraj

    Code Below
    ========================
    [COLOR=blue]
    <SCRIPT LANGUAGE=javascript>
    <!--
    function init()
    {
    //Getting from the server names of the Controls
    var arr=new Array("txt","txtarea","s1","r1","c1")
    for(i=0;i<arr.length;i++)
    {
    obj=eval("document.forms[0]." + arr[i]);
    alert(obj.type);
    }
    }
    onload=init;
    //-->
    </SCRIPT>

    </HEAD>
    <BODY>
    <form>
    <!--Dynamically Generated Controls from the Server-->
    <input type=text name=txt>
    <textarea name=txtarea></textarea>
    <select name=s1>
    <option>value1
    <option>value2
    <option>value3
    <option>value4
    </select>
    <input type=radio name=r1>R1
    <input type=radio name=r1>R1
    <input type=radio name=r1>R1
    <input type=checkbox name=c1>Checkbox
    <!-- Other controls need not to be worried-->
    <input type=text name=txt1>
    <input type=text name=txt2>
    <input type=text name=txt3>
    <input type=text name=txt4>


    </form>

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,968
    Thanks
    0
    Thanked 236 Times in 233 Posts
    you can implement it this way easily: (same code I previously posted)

    <script>
    function init()
    var f = document.forms[0];
    for (var i=0;i<f.elements.length;i++){
    alert(f.elements[i].name+ ": " +f.elements[i].type)
    }
    onload=init;
    </script>

  • #9
    New Coder
    Join Date
    Oct 2002
    Location
    Bangalore,India
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi
    I don't want to access all the Controls in the HTML but only few controls which are generated dynamically from the server.
    those names are sent by javascript array.

    Thanks
    anandraj

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,968
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Code:
    var arr=new Array("txt","txtarea","s1","r1","c1") 
    for(i=0;i<arr.length;i++) 
    { 
    	obj=document.forms[0].elements[arr[i]];
    	if (obj.length){ //this must be a radio button
    		for (j=0;j<obj.length;j++){
    			alert(obj[j].name + ":" + obj[j].type)
    		}
    	}
    	else alert(obj.name + ":" + obj.type); 
    }

  • #11
    New Coder
    Join Date
    Oct 2002
    Location
    Bangalore,India
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi
    Thanks. I thought of doing that finally. But it will return the length for Combo also inside i have to check for the Select-one or select-multiple also right. Is there any other method to do this?

    Thanks
    Anandraj

  • #12
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,968
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Code:
    var arr=new Array("txt","txtarea","s1","r1","c1") 
    for(i=0;i<arr.length;i++) 
    { 
    	obj=document.forms[0].elements[arr[i]];
    	if (obj.length && obj[0].type=="radio"){ //this is a radio button
    		for (j=0;j<obj.length;j++){
    			alert(obj[j].name + ":" + obj[j].type)
    		}
    	}
    	else alert(obj.name + ":" + obj.type); 
    }


  •  

    Posting Permissions

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