Jump to content

Limiting upload slots with append


squigs

Recommended Posts

Hope everyone is doing well,

I'm trying to append items with javascript but limit the amount of times they can be appended. Currently I am using this to add the items with no limit.

$(".add").click(function() {
      $("td.files").append('<tr><td><input type="file" name="uploaded_file[]" class="file_input" size="46" style="margin-top:3px;"/><a class="delete" href="javascript:void(0)" style="color:blue; padding-left:15px;">Remove File</a></td></tr>');
});

I have been playing with the following but am unable to get it functioning:

var count = 0;
$(".add").onclick = function(e) {
    if (count >= 4) {
        return false;
    }
    else {
        count++;
        $("td.files").innerHTML += '<tr><td><input type="file" name="uploaded_file[]" class="file_input" size="46" style="margin-top:3px;"/><a class="delete" href="javascript:void(0)" style="color:blue; padding-left:15px;">Remove File</a></td></tr>';
    }
};

I have also tried assigning IDs instead of classes and using document.getElementById to no avail.  :wtf: am I doing wrong here?

 

Thanks

Link to comment
Share on other sites

Got it sorted out! I ended up using this to get my desired result.

var addFile = document.getElementById('addFile');
addFile.onclick = function addFile() {
    if (this.count == undefined) {
        this.count = 0;
    }

    if (this.count++ < 4) {
        $("td.files").append('<tr><td><input type="file" name="uploaded_file[]" class="file_input" size="46" style="margin-top:3px;"/><a class="delete" href="javascript:void(0)" style="color:blue; padding-left:15px;">Remove File</a></td></tr>');
    }
};

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.