PDA

View Full Version : HTML Controls


anandraj
10-17-2002, 06:09 AM
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

glenngv
10-17-2002, 07:19 AM
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

adios
10-17-2002, 07:27 AM
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.

glenngv
10-17-2002, 07:45 AM
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.

anandraj
10-17-2002, 08:27 AM
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

glenngv
10-17-2002, 09:00 AM
post the code pls

anandraj
10-17-2002, 09:14 AM
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>

glenngv
10-17-2002, 09:28 AM
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>

anandraj
10-17-2002, 09:31 AM
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

glenngv
10-17-2002, 09:39 AM
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);
}

anandraj
10-17-2002, 09:58 AM
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

glenngv
10-17-2002, 10:14 AM
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);
}