welcome, anonymous.

[ login ]

browse

2 items tagged: javascript

filter: links


javascript bookmarks

posted by sick 1 year ago (May 28, 07).
link  

unlimited file uploads

<html>
<head>
<style>
#attachments { border: 1px solid gray; padding: 5px; }
#attachments div { margin-top: 5px; margin-bottom: 5px; }
#attachments a { text-decoration: none; color: red; }
</style>
</head>
<body>

<script type="text/javascript" language="Javascript">
// <![CDATA[

// 'Unlimited' file uploads widget function
// Gifted to the public domain by <mreece@brainspun.com>
//
// Usage: 
//  <div id="attachments" style="display:none;"></div>
//  <a href="#" onclick="javascript:addAttachment('attachments');">
//      Add attachment</a>
// 
// Clicking the link (that is, calling addAttachment(container_id)) will 
//   append an additional file input to the container div, along with a
//   'remove' link for removing the file input.
// The <input type="file" will have name="attach.n" where n is >= 1
// The 'container' div will be set to 'display:block' when an attachment
//   is added, and 'display:none' if all attachments are removed.

var attach_id    = 1;  // counter for name="attach.n"
var attach_count = 0;  // count of un-removed file inputs
function addAttachment(container_id) {
    var container = document.getElementById(container_id);

    // set up the <input type="file" name="attach.n"/>
    var input = document.createElement('input');
    input.type = 'file';
    input.name = 'attach.' + attach_id++;

    // set up the <a href="#" onclick=removeAttachment()>remove</a>
    var remove = document.createElement('a');
    remove.appendChild(document.createTextNode('remove'));
    remove.href = '#';
    remove.onclick = function() {
        container.removeChild(this.parentNode);
        if ( --attach_count < 1 ) {
            container.style.display = 'none';
        }
        return false;
    };

    // set up new div to hold the input and remove link
    var div = document.createElement('div');
    div.appendChild(input);
    div.appendChild(remove);

    // and inject it into the container
    container.appendChild(div);
    if ( ++attach_count > 0 ) {
        container.style.display = 'block';
    }

    return false;
}
// ]]>
</script>

<div id="attachments" style="display:none"></div>
<a href="#" onclick="javascript:addAttachment('attachments');">Add attachment</a>

</body>
</html>
tags: javascript
posted by sick 2 years ago (Jun 29, 06); edited 2 years ago (Jun 29, 06).
link