Solved

count of the number of file elements that have a value

Posted on 2012-03-13
5
276 Views
Last Modified: 2012-03-14
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!
0
Comment
Question by:jagku
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 9

Expert Comment

by:experts1
ID: 37716756
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

0
 

Author Comment

by:jagku
ID: 37716985
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
0
 
LVL 9

Assisted Solution

by:experts1
experts1 earned 200 total points
ID: 37717111
Modify javascript as below for FILE type:
<script type="text/javascript">
function checkInput()
{
                var checkIn = document.getElementsByTagName("input");
                var incount = 0;
                var valStr = "";

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

Open in new window

0
 
LVL 9

Assisted Solution

by:experts1
experts1 earned 200 total points
ID: 37717165
Please correct line below:

valStr = valStr +"\n INPUT ["+x+"] Value = " +checkIn[x].value;
0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 300 total points
ID: 37717698
Have a look here : http://jsfiddle.net/4gL6x/
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

731 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question