Jump to content

Counting number of times a function has run


magicmoose

Recommended Posts

Hi,

I'm using PHP to upload an unset number of images in a form.  I'm trying to use javascript to add more form elements in order to allow the correct number of images to be uploaded.

<script language="javascript" type="text/javascript">
function addContent(divName) {
content = '<label for=\"file1\">File to upload:</label><input id=\"file1\" type=\"file\" name=\"userfile[]\" /><br />';
document.getElementById(divName).innerHTML += content;
}
</script>

 

 

 

 

<li onclick="addContent('additemform');">More</li>

 

The problem is, I need to replace the numbers (eg file1), with the number of times the function has been run, otherwise each file will have the same id.  Is there a simple way to do this? 

Thanks.

 

 

<script language="javascript" type="text/javascript">
var count = 0;
function addContent(divName) {
var content = '<label for="file' + count + '">File to upload:</label><input id="file' + count + '" type="file" name="userfile[]" /><br />';
document.getElementById(divName).innerHTML += content;
count = count+1;
}
</script>

 

Something like that?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.