Author Topic: JS help...  (Read 455 times)

Offline Sim

  • Level 13
  • *
  • Posts: 104
  • Reputation: +1/-1
    • View Profile
    • Online RPG Creator
JS help...
« on: February 09, 2011, 04:13:10 PM »
I am trying to add a new upload field to my form. Its not working to well ;(. Here's what I got.

my JS:

Code: [Select]
<script type="text/javascript" language="javascript">    function addNewUploadField() { document.getElementById("files").appendChild("Test<br>"); } </script>
my HTML:
Code: [Select]
<div id="files"></div>
<tr>
  <td><strong></strong></td>
  <td><label><input type="submit" name="SubmitForm" value=" Go " /></label></td>
</tr>
  </table>
</form>
<a href="#" onclick="addNewUploadField()">Add Another File</a>

Offline LuckyD0g

  • Level 2
  • *
  • Posts: 4
  • Reputation: +0/-0
    • View Profile
Re: JS help...
« Reply #1 on: February 09, 2011, 05:01:47 PM »
This should work

Code: [Select]
<script type="text/javascript" language="javascript">
    function addNewUploadField() { document.getElementById("files").innerHTML+="Test<br>"; }
</script>

Offline Sim

  • Level 13
  • *
  • Posts: 104
  • Reputation: +1/-1
    • View Profile
    • Online RPG Creator
Re: JS help...
« Reply #2 on: February 10, 2011, 09:41:59 AM »
Heres how I did it

HTML:
Code: [Select]
            <form name="form1" id="form1" method="post" action="" enctype="multipart/form-data">
            <input id="fileCount" type="hidden" name="hidden" value="2" />
            <table width="100%" border="1" id="fileTable">
                <tr>
                    <td>
                        <strong>Image #1</strong>
                    </td>
                    <td>
                        <label>
                            <input type="file" name="file1" id="file1" /></label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <strong>Image #2</strong>
                    </td>
                    <td>
                        <label>
                            <input type="file" name="file2" id="file2" /></label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <strong></strong>
                    </td>
                    <td>
                        <label>
                            <input type="submit" name="SubmitForm" value=" Go "></label>
                    </td>
                </tr>
            </table>
            </form>
            <a onclick="addNewUploadField()">Add Another File</a>

JS
Code: [Select]
var imgIndex = 3;

function addNewUploadField() {
    $('#fileTable tr:last').before('<tr><td><strong>Image #' + imgIndex + '</strong></td><td><label><input type="file" name="file' + imgIndex + '" id="file' + imgIndex + '" /></label></td></tr>');
    $('#fileCount').val(imgIndex);
    imgIndex++;
}

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: JS help...
« Reply #3 on: February 10, 2011, 11:23:29 AM »
It's much more convenient to use name="someName[]" ... then the contents are automatically put into array and there's also no need for name="fileCount" since you can use count()
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal