Solved

count of the number of file elements that have a value

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now