Link to home
Start Free TrialLog in
Avatar of jagku
jagkuFlag for United States of America

asked on

count of the number of file elements that have a value

Hello Experts,

Probably being thick - but here goes:

I have a few of these html inputs:

<form name="myform">
<input type="file" name="myfile[]" />
<input type="file" name="myfile[]" />
<input type="file" name="myfile[]" />
</form>

Open in new window


Using javascript, how do I output
a) the number of elements (3 in this case)
b) the values assigned to these elements.

I tried:

var files =document.myform.elements["myfile[]"];				
for (i = 0; i < files.length; i++)
{   			
    filename = files[i].value;
}

Open in new window


However, the file length is undefined.
I cannot change the name of the input variable from myfile[] for legacy reasons.

Many Thanks!
Avatar of experts1
experts1

Try mod below:
<head>
    <meta charset="UTF-8" />
    <title>File Input</title>
	<script type="text/javascript">
function checkInput()
{
                var checkIn = document.getElementsByTagName("input");
                var incount = 0;
                var valStr = "";

                for (var x = 0; x < checkIn.length; x++)
                {
                       valStr = valStr +"\n INPUT ["+incount+"] Value = " +checkIn[incount].value;
                       incount += 1;
                }
                alert("INPUT BOXES = " + incount+valStr);
}
</script>
    
</head>
<body onload="checkInput();">

<form name="myform">
<input type="file" name="myfile[]"  />
<input type="file" name="myfile[]"  />
<input type="file" name="myfile[]"  />
</form>
</body>
</html>

Open in new window

Avatar of jagku

ASKER

Hi,

Thanks.
How can I restrict it to file elements.

I don't mind if it is referenced as

name=myfile[]

or

type=file

Thanks
SOLUTION
Avatar of experts1
experts1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Albert Van Halen
Albert Van Halen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial