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.

 

 

Link to comment
Share on other sites

<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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.