0

I'm using jQuery.filerhttps://github.com/CreativeDream/jQuery.filer to upload images in edit part (this is in separate JavaScript file) below method they are getting data. I'm using MySQL to store image names:

files: [
    {
        name: image1,
        size: 5453,
        type: "image/jpg",
        file: "uploads/" + image1
    }, {
        name: image2,
        size: 5453,
        type: "image/jpg",
        file: "uploads/" + image2
    }
]

I need to add a loop on this to get available names that are not empty.

I'm using document.getElementById:

var image1 = document.getElementById('image1').value;
var image2 = document.getElementById('image2').value;
var image3 = document.getElementById('image3').value;
var image4 = document.getElementById('image4').value;
var image5 = document.getElementById('image5').value;
var image6 = document.getElementById('image6').value;
var image7 = document.getElementById('image7').value;
var image8 = document.getElementById('image8').value;
var image9 = document.getElementById('image9').value;
var image10 = document.getElementById('image10').value;  

this is there function in filer.js

_filesCheck: function() {
                        var s = 0;
                        if(n.limit && f.files.length + f._itFl.length > n.limit) {
                            alert(f._assets.textParse(n.captions.errors.filesLimit));
                            return false
                        }
                        for(var t = 0; t < f.files.length; t++) {
                            var x = f.files[t].name.split(".")
                                .pop()
                                .toLowerCase(),
                                file = f.files[t],
                                m = {
                                    name: file.name,
                                    size: file.size,
                                    size2: f._assets.bytesToSize(file.size),
                                    type: file.type,
                                    ext: x
                                };
                            if(n.extensions != null && $.inArray(x, n.extensions) == -1) {
                                alert(f._assets.textParse(n.captions.errors.filesType, m));
                                return false;
                                break
                            }
                            if(n.maxSize != null && f.files[t].size > n.maxSize * 1048576) {
                                alert(f._assets.textParse(n.captions.errors.filesSize, m));
                                return false;
                                break
                            }
                            if(file.size == 4096 && file.type.length == 0) {
                                return false;
                                break
                            }
                            s += f.files[t].size
                        }
                        if(n.maxSize != null && s >= Math.round(n.maxSize * 1048576)) {
                            alert(f._assets.textParse(n.captions.errors.filesSizeAll));
                            return false
                        }
                        if((n.addMore || n.uploadFile)) {
                            var m = f._itFl.filter(function(a, b) {
                                if(a.file.name == file.name && a.file.size == file.size && a.file.type == file.type && (file.lastModified ? a.file.lastModified == file.lastModified : true)) {
                                    return true;
                                }
                            });
                            if(m.length > 0) {
                                return false
                            }
                        }
                        return true;
                    },

Please let me know how to add to this array which is only available?

user1750832
  • 75
  • 1
  • 10
  • you want to achieve a loop which loops over all images? so there could be only 3 images as well as 100 - a dynamic number? – messerbill Mar 23 '16 at 13:17
  • You might want to post some html so that we can craft a solution that works with your markup. If you want to loop over something, then you'd want to use classnames or structure to determine inputs rather than ids. – Mark Evaul Mar 23 '16 at 13:20
  • exactly messerbill i want to get like that dynamically – user1750832 Apr 02 '16 at 08:17

1 Answers1

1

If you want to loop over all elements in your page not knowing their exact quantity, but knowing that their ids start with the word image you can do this (jQuery):

$('[id^=image]').each(function() {
    var $el=$(this);

    // do whatever you need with the element stored in $el
});
dekkard
  • 6,121
  • 1
  • 16
  • 26
  • shiny solution :) you also could use css classes and access them via `$('.className').each(function()...)` – messerbill Mar 23 '16 at 13:29
  • how do i add files[ ] part in to this – user1750832 Apr 02 '16 at 08:20
  • This code iterates DOM nodes with specific IDs (similar to what you tried to achieve with ```getElementById()```). On how to loop through a JS array check this: http://stackoverflow.com/questions/3010840/loop-through-array-in-javascript – dekkard Apr 02 '16 at 08:28
  • dekkard i have added main function their using for send file from ready.function can u suggest me how to send to that – user1750832 Apr 02 '16 at 08:34