0

Hi
I want to extract all the names properties of the html form. I have 2 elements (Textarea and CheckBox) and I need to create 2 arrays with all the names of them, is to say, in the array1 all the checkbox names and in the array2 all the textarea names.
In this code I get the names but it print undefined values because the length of the arrays is the double size.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
 
 </HEAD>

 <BODY>
 <FORM METHOD=POST ACTION="" name="solicitud" id="frm1">
	
 <TABLE>
 <TR>
	<TD> <TEXTAREA id='text' NAME="text1"  ></TEXTAREA></TD>
	<TD> <INPUT id='check' TYPE="checkbox" NAME="check1"></TD>
 </TR>
 <TR>
<TD> <TEXTAREA id='text' NAME="text2" ROWS="" COLS="" disabled></TEXTAREA></TD>
	<TD> <INPUT id='check' TYPE="checkbox" NAME="check2"></TD>
 </TR>
 <TR>
	<TD> <TEXTAREA id='text' NAME="text3" ROWS="" COLS="" disabled></TEXTAREA></TD>
	<TD> <INPUT id='check' TYPE="checkbox" NAME="check3"></TD>
 </TR>
 <TR>
<TD> <TEXTAREA id='text' NAME="text4" ROWS="" COLS="" disabled='disabled'></TEXTAREA></TD>
	<TD> <INPUT id='check' TYPE="checkbox" NAME="check4" ></TD>
 </TR>
 </TABLE>
 </FORM>

 
 <script type="text/javascript">

var x=document.getElementById("frm1");
var j=x.length;
var texto=document.getElementsByTagName("TEXTAREA");
var check=document.getElementsByTagName("INPUT");
var jhon=new Array();
var jhon1=new Array();

alert(texto.length+" "+check.length);

for (var i=0;i<j;i++)
  {
  if(x.elements[i].id=="check"){
	jhon[i]=x.elements[i].name;
  }else if(x.elements[i].id=="text"){
	jhon1[i]=x.elements[i].name;
  }
  }

for (var r=0;r<jhon.length;r++)
  {
	 document.write(jhon[r]+"  "+ jhon1[r]);
	 document.write("<br />");
  }
alert(jhon.length);
alert(jhon1.length);

</script>

 </BODY>
</HTML>

I use a conditional for filter it but not work.
Someone have any tips for me, please.

Muchas Gracias

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by jhonilson
0

The problem is that you can't directly access attribute 'name' using '.name' in FF (not sure in IE). You need to use function getAttribute(attributeName, flag) instead. The 'flag' value can be left out.

if(x.elements[i].id=="check") {
  jhon[i]=x.elements[i].getAttribute("name");
} else if(x.elements[i].id=="text"){
  jhon1[i]=x.elements[i].getAttribute("name");
}

Hope this help.

0

Thanks Taywin. i resolved the problem in this way:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>

 <head>   
    <script type="text/javascript">   
        function Init () {   
            var form = document.getElementById ("frm1");   
            var areas = form.getElementsByTagName ("textarea");   
            var chkBoxes = form.getElementsByTagName ("input"); 
			var nombre1;
			var nombre;
  
            var areaNames = [];   
            for (var i = 0; i < areas.length; i++) { 
				
                areaNames.push (areas[i].name);   
            }   
			
            var chkBoxNames = [];   
            for (var i = 0; i < chkBoxes.length; i++) {   
                if (chkBoxes[i].type == "checkbox") {  
                    chkBoxNames.push (chkBoxes[i].name);   
                }   
            }   

        alert (areaNames);   
      alert (chkBoxNames);   	 
	
}	
    </script>   
</head>   
<body onload="Init()">   
    <form method=post action="" name="solicitud" id="frm1">         
        <table>      
            <tr>      
                <td><textarea name="text1" disabled="disabled"></textarea></td>      
                <td><input type="checkbox" name="check1"  ></td>      
            </tr>      
            <tr>      
                <td><textarea name="text2" disabled="disabled"></textarea></td>      
                <td><input type="checkbox" name="check2" ></td>      
            </tr>      
            <tr>      
                <td><textarea name="text3" disabled="disabled"></textarea></td>      
                <td><input type="checkbox" name="check3" ></td>      
            </tr>      
            <tr>      
                <td><textarea name="text4" disabled="disabled"></textarea></td>      
                <td><input type="checkbox" name="check4" ></td>      
            </tr>   
        </table>   

    </form>      
</body>  

</HTML>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.