Solved

count of the number of file elements that have a value

Posted on 2012-03-13
5
272 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
  • 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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. …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

786 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