1

In a nutshell,

Below is what I am trying to accomplish: http://jsfiddle.net/n3r8conn/8/

but as shown I am having issues, it would work as follow: User clicks on button once, the file gets selected from input and then display in the files_1 id, user clicks on button again, file gets selected, and then output into files_id2,

This part only shows the upload part, in other words, everything mention above beside displaying the image on screen.

Html COde:

 <button id="uploadDevice" class="button button-block button-positive">
            <i class="icon ion-android-mail"></i> &nbsp; <text id="textBtn1">From Device </text>
</button>
                <input type="file" class="uploadDevice" id="files_1" name="image_file_arr[]" multiple>
                <input type="file" class="uploadDevice" id="files_2" name="image_file_arr[]" multiple>
                <input type="file" class="uploadDevice" id="files_3" name="image_file_arr[]" multiple>

JavaScript Code:

$('#uploadDevice').click(function(){
    myGlobalCounter++;
   $( '#files_' +myGlobalCounter ).val('Secret text ' + myGlobalCounter);
});

CSS code:

 .uploadDevice{
     visibility : hidden;
  }

Update:

<ion-content>
    <ion-slide-box id="uploadedPictures" on-slide-changed="slideHasChanged($index)">
          <ion-slide>

<output id="profilePic1"></output>

  </ion-slide>
          <ion-slide>
<output id="profilePic2"></output>

  </ion-slide>
          <ion-slide>
<output id="profilePic3"></output>

  </ion-slide>
</ion-slide-box>


<div class="row">
<div class="col col-50">
     <button id="uploadFacebook" class="button button-block button-positive">
         <i class="icon ion-social-facebook"></i> &nbsp; <text id="textBtn1"> From Facebook 
</button>
</div>         



<div class="col col-50"><a href="javascript:void(0)" id="buttonFile" href=""> 
 <button id="uploadDevice" class="button button-block button-positive">
            <i class="icon ion-android-mail"></i> &nbsp; <text id="textBtn1">From Device </text>
</button></a>
                <input type="file" class="uploadDevice" id="files" name="image_file_arr[]" multiple>


    </div>
  </div>

  <style>
  .uploadDevice{
     visibility : hidden;
  }
  </style>

  <script>
  $("#buttonFile").click(function(){
        $("#files").trigger('click');
    });

    </script>

script

function handleFileSelect(evt) {
    var $fileUpload = $("input#files[type='file']");
    if (this.files.length > 4) {
        alert("You can only upload a maximum of 5 files");
        evt.preventDefault();
        evt.stopPropagation();
        return false;
    }
    var files = this.files;
    for (var i = 0, f; f = files[i]; i++) {
            if (!f.type.match('image.*')) {
                continue;
            }
        (function(i){
            var reader = new FileReader();

            reader.onload = (function (theFile) {
                return function (e) {
                    var span = document.createElement('span');
                    span.innerHTML = ['<img id="userPictures" src="', e.target.result, '" title="', escape(theFile.name), '"/>'].join('');
                    document.getElementById('profilePic' + (i + 1)).appendChild(span);
                };
            })(f);

            reader.readAsDataURL(f);
        })(i);
    }
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
Jonathan Etienne
  • 621
  • 3
  • 6
  • 18

4 Answers4

1

Here's another version of the 3rd answer.

This one just moves each FILE INPUT after you click on it, so you can see that the next FILE INPUT is now in place to be clicked.

Basically, instead of using z-index to rotate the just-clicked FILE INPUT to the bottom of the stack, I am moving it down the page so you can visually see what is happening.

jsFiddle Demo

Code:

var xx, global_cnt = 1;

$('#clicker').button(); //use jQueryUI to auto-style the button

$('div').click(function(){
    xx = global_cnt * 60;
    $('#real_uploader_' +global_cnt).css({'position':'absolute','top':xx+'px'});
    global_cnt++;

    if ( $('#real_uploader_' +global_cnt).length ){
        $('#real_uploader_' +global_cnt).css('z-index','2');
    }
});
cssyphus
  • 37,875
  • 18
  • 96
  • 111
0

Something like this?

jsFiddle Demo

HTML:

<input type="text" class="th" id="typeHidden_1" />
<input type="text" class="th" id="typeHidden_2" />
<input type="text" class="th" id="typeHidden_3" />
<input type="text" class="th" id="typeHidden_4" />
<input type="button" id="mybutt" value="Show Me" />
<input type="button" id="nutherbutt" value="Read Hidden Field" />

javasccript/jQuery:

var myGlobalCounter = 0;

$('#mybutt').click(function(){
    myGlobalCounter++;
   $( '#typeHidden_' +myGlobalCounter ).val('Secret text ' + myGlobalCounter);
});

$('#nutherbutt').click(function(){
    for (n=1; n<= myGlobalCounter; n++){
        var tmp = $('#typeHidden_' +n).val();
        alert( tmp );
    }
});
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Thanks for your response. Something like this, but I am using input type file instead and for some reason i seem to be getting an error http://jsfiddle.net/n3r8conn/8/ – Jonathan Etienne Mar 10 '15 at 23:04
0

I apologize -- I did not read your answer carefully enough the first time.

The <input type="file" /> element is special. For security reasons, there is very little you can do with javascript to control a file input element. You cannot set the filename programmatically, and you cannot click the button programmatically.

However, there is a work-around that is frequently proposed:

HTML:

<div>
    <button><a href="javascript: void(0)">Upload File</a></button>
    <input type="file" id="upload_input" name="upload" />
</div>

CSS:

div{display:block;width:100px;height:20px;overflow:hidden;}

button{width:110px;height:30px;position:relative;top:-5px;left:-5px;}

input{font-size:50px;width:120px;opacity:0;filter:alpha(opacity:0); position:relative;top:-40px;left:-20px;background:yellow;}

non-working jsFiddle with code


References:

In JavaScript can I make a "click" event fire programmatically for a file input element?

Programmatically set the value of a type="file" input HTML element?

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • ok thanks for the explanation. so lets see for a work around. I have an input file here, where when the user selects a picture its get displayed to a different ID up to 3 times. Like theres 3 output, and each output relates to an id, the problem i have is that it only outputs to the first one, and any other pictures fall right beside it and not at a different id. I have included an update under my initial postr – Jonathan Etienne Mar 10 '15 at 23:43
  • By the way -- this may not be relevant, but -- the most useful file upload utility of which I am aware, is Ravishanker Kusuma's `jQuery File Upload plugin`: http://hayageek.com/docs/jquery-upload-file.php – cssyphus Mar 10 '15 at 23:47
  • Okay, this probably isn't what you are asking either, but I re-styled (improved) the example in the above answer: http://jsfiddle.net/nyvkzjba/1/ – cssyphus Mar 10 '15 at 23:49
  • @JonathanEtienne See the third answer to this question (also by me) -- it may be closer to what you want. – cssyphus Mar 11 '15 at 01:41
  • thank you this should help do the trick. appreciate. sorry this my small last request, how do I set the width of an item in javascript in terms of % for example http://jsfiddle.net/UTp4T/43/ I want the width of the image to be 100% – Jonathan Etienne Mar 11 '15 at 03:15
  • Try this: `.attr("width", "100%")` *These are css statements, so they need either `px` or `%` or `em` after them.* - http://jsfiddle.net/UTp4T/45/ – cssyphus Mar 11 '15 at 03:37
  • thats my concern when i add % it doesnt load – Jonathan Etienne Mar 11 '15 at 03:38
  • It should... it's just a `css` parameter. `100%` should work -- keep fiddling with it... – cssyphus Mar 11 '15 at 03:39
  • 1
    @JonathanEtienne - **OH WAIT** -- *if the image is larger than the container, the image will be invisible...* Maybe that's what's happening...? – cssyphus Mar 11 '15 at 03:41
  • how would I have the container be 100% as well>? – Jonathan Etienne Mar 11 '15 at 03:43
  • ah, yeah, there's a problem. You know what? You're really limited with `li`, `ul` and (unrelated, but important) `span`. Very difficult to manipulate height, width, margin-top, etc. with these elements. *I suggest switching to DIVs instead* Honestly, they are just as easy to work with once you get started -- and *so much more *stylable** – cssyphus Mar 11 '15 at 03:47
  • thanks. I follow your advice and made some minor adjustments. but the problem persist : http://jsfiddle.net/UTp4T/52/ – Jonathan Etienne Mar 11 '15 at 03:51
  • Okay, if I understand your code, your AJAX block gets back some data: `result`. What data type is it? If JSON, you need to convert it into an object with `var myObj = JSON.parse(result);` -- but the `result` data *must* be valid JSON, so be sure of that. Next, where did `this.image` come from? What is `this` in the context? How did you get `image` out of the `result` data? *I think you have to either (1) receive the data in text/html and break it into strings, or (2) receive the data (`result`) as json and parse it into an object/array.* – cssyphus Mar 11 '15 at 03:58
  • I think you are correct, 2) receive the data (result) as json and parse it into an object/array. this.image is referring to the images defined – Jonathan Etienne Mar 11 '15 at 04:00
  • Yup, I know what you mean. I was asking how you get the actual *value* - what does the variable `this.image` contain? I don' think it has any value yet... that's what I'm saying. :) – cssyphus Mar 11 '15 at 04:02
0

Let me try again.

I was having some difficulty understanding what you need. If I can get my head around that, then I'm pretty sure I can help.

Please check if this is what you need.

This code has 3 <input type="file" > elements. All 3 are "invisible" (using opacity, to they remain where they are on screen, they are just invisible).

The FILE inputs are also stacked on top of each other. At the start, input_1 is on top --- but it is invisible, so all you see is the BUTTON underneath it.

When "the button" (really, input_1) is clicked, three things happen:

(1) the OPEN dialog (Choose a file to upload) is displayed for input_1

(2) global_cnt is used to move input_1 to the back (using z-index)

(3) global_cnt is incremented, and then used again to move input_2 to the front

(4) global_cnt is now at the correct number to move input_2 to back when it is clicked

Each time the user clicks "the button" (really, they are clicking an invisible input type="file" element), a different input element is moved to the top. Therefore, by clicking the same "button", the user keeps uploading to a different file input.

Note: this works because when you click input_1, the div also gets the click event (through event bubbling).

jsFiddle Demo code

Code:

HTML:

<div>
    <input type="button" id="clicker" value="Upload File" />
    <input type="file" class="hidden_uploaders" id="real_uploader_1" name="upload_1" />
    <input type="file" class="hidden_uploaders" id="real_uploader_2" name="upload_2" />
    <input type="file" class="hidden_uploaders" id="real_uploader_3" name="upload_3" />
</div>

javascript/jQuery:

var global_cnt = 1;

$('#clicker').button(); //use jQueryUI to auto-style the button

$('div').click(function(){
    if ( $('real_uploader_' +global_cnt).length ) $('real_uploader_' +global_cnt).css('z-index','-1');
    global_cnt++;
    if ( $('real_uploader_' +global_cnt).length ) $('real_uploader_' +global_cnt).css('z-index','2');
});

still working on this

cssyphus
  • 37,875
  • 18
  • 96
  • 111