Avatar of jagku
jagku
Flag 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!
JavaScript

Avatar of undefined
Last Comment
Albert Van Halen

8/22/2022 - Mon
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

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
experts1

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
experts1

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Albert Van Halen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23