-1

<input> for select images:

<input type="file" class="form-control forminputs files photoimg" multiple name="photoimg[]" placeholder="Choose 3 image files" accept="image/png,image/jpeg" id="photoimg" /> 

I am trying to select at least 3 images. If it is exceeded 3, then it alerts "please select 3" and again automatically opens file browse window.

Up to alert everything works fine but after that file browse window is not opening.

JS code:

$('#photoimg').change(function() {
    var files = this.files.length;
    if (files > 3)
    {
        alert("Please select 3 images only(png/jpg)");
        document.getElementById("photoimg").click();
    } 
});

tried with this.click() and $('#photoimg').click();.

 $('#photoimg').click(function() {
     alert("clicked");
     });

to test it click event was taken, click event was working but still file browse was not working. Why it is not working?

6
  • instead of if(files>3) try if(paresInt(files)>3) Commented Nov 13, 2014 at 9:12
  • @Kartikeya but alert is working Commented Nov 13, 2014 at 9:14
  • instead of document.getElementById("photoimg").click(); try $(this).click() Commented Nov 13, 2014 at 9:15
  • Do you get any console error? Commented Nov 13, 2014 at 9:15
  • 2
    @jaya AFAIK, you cannot do it without any user interaction (click) Commented Nov 13, 2014 at 9:21

2 Answers 2

0
Here is the working demo
$('#photoimg').change(function() {
var files = this.files.length;
if (files != 3)
{
    alert("Please select 3 images only(png/jpg)");
    document.getElementById("photoimg").click();
} 
});

http://jsfiddle.net/silpa/4fbz5f1m/4/

Sign up to request clarification or add additional context in comments.

1 Comment

upto alert it is working fine but not opened file browse window
0

You can't perform click event for file input because of security reasons. You can do that by creating custom element which behave like file input element.

1 Comment

You can do that by creating custom element which behave like file input element How that's relevant to OP's question? Any sample?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.