3

/note: this only applies when used on the specified domain, but works practically/

I've been trying to simulate a mouse click event, it works for anything else but not for this..

I tested it, it is working now somehow.. but whenever I try to run on this website: http://www.multiplayerpiano.com/ it seems not to work...

<input type="file" id="_LOAD" accept="mp3" />

$('#_LOAD').click();

this doesn't work..

I've also tried

$('#_LOAD')[0].click();

no luck..

can anyone please explain what is it that is blocking it on That specific domain?

// edit - original code

I was trying to make a function which will allow me to make file dialog box's which only allows a specific mime type to be allowed

var load = function(mime,callback){

    var tempmime = (mime) ? mime : "";

    var tempinp = $('<input style="position:absolute;opacity:0;z-index:-1;pointer-events:none" type="file" id="_LOAD" ' + (tempmime == "" ? "" : 'accept=".' + tempmime + '"') + '/>')

    $("body").append(tempinp);
    $(tempinp).focus().click();
// 
    var aab = function(msg){
    callback(msg);
         $(tempinp).off('change',aab);
        $(tempinp).remove();
    };
    $(tempinp).on('change',aab);
}

So this is as far i have got without .click working.

4
  • What do you mean by "it doesn't work" ? Commented Sep 22, 2015 at 13:50
  • It is not triggering the click event. Commented Sep 22, 2015 at 13:51
  • 2
    jsfiddle.net/arunpjohny/nxzsqdho/1 - I think the browser is blocking the default behavior if the click is triggered from a non user initated method Commented Sep 22, 2015 at 13:52
  • 1
    stackoverflow.com/questions/210643/… Commented Sep 22, 2015 at 14:30

2 Answers 2

2

can you try

  $("#_LOAD").trigger('click');
Sign up to request clarification or add additional context in comments.

1 Comment

can you try with variable name like $(tempinp).trigger('click');
1

try this:

document.getElementById("_LOAD").click();

Also you can check if you are selecting elements with

console.log("Element exists : " + (document.getElementById("_LOAD") != null ) );    

1 Comment

document.getElementById() returns a unique element. Also, the <input> element as no length property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.