7

I'm trying to simulate a click on an input tag, through the click on an anchor tag, this way I can hide the input and wrap an image inside the anchor tag.

This works using the jQuery trigger function, but I can't make it work with just "plain" Javascript:

jQuery version:

let fake = $('.fake')
fake.click(function(e) {
  e.preventDefault();
  $('#user_avatar').trigger('click');
})
#user_avatar { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="file" name="file_field" id="user_avatar">
<a href="#" class="fake">
  <img src="https://fthmb.tqn.com/65lNzIRNfZY4xY02D17b1RcGvso=/960x0/filters:no_upscale()/kitten-looking-at-camera-521981437-57d840213df78c583374be3b.jpg" width="320" height="240">
</a>

The JavaScript version using new Event and dispatchEvent:

let fake = document.querySelector('.fake');
fake.addEventListener('click', function(e) {
  e.preventDefault();
  console.log('testing');
  let clickEvent = new Event('click');
document.getElementById('user_avatar').dispatchEvent(clickEvent)
})
#user_avatar { display: none; }
<input type="file" name="file_field" id="user_avatar">
<a href="#" class="fake">
  <img src="https://fthmb.tqn.com/65lNzIRNfZY4xY02D17b1RcGvso=/960x0/filters:no_upscale()/kitten-looking-at-camera-521981437-57d840213df78c583374be3b.jpg" width="320" height="240">
</a>

The console.log is rendered, but the event isn't being dispatched, what am I doing wrong?

Karol Karol
  • 566
  • 3
  • 7
  • 26
  • 1
    What about `document.getElementById('user_avatar').click() `? – DontVoteMeDown Oct 18 '17 at 17:40
  • Why dont you style the input to look like an image instead? – izulito Oct 18 '17 at 17:41
  • Shameless plug: [generic synthetic event dispatching function that works on all HTML elements](https://stackoverflow.com/questions/2381572/how-can-i-trigger-a-javascript-event-click/2381862#2381862) ... (not just on input/links like the `click()`) method – Ruan Mendes Oct 18 '17 at 17:46

3 Answers3

11

Use:

document.getElementById('user_avatar').click();

Tested and it works.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Sam
  • 608
  • 4
  • 11
5

document.getElementById('user_avatar').click() will work

let fake = document.querySelector('.fake');
fake.addEventListener('click', function(e) {
  e.preventDefault();
  document.getElementById('user_avatar').click()
})
#user_avatar {
  display: none;
}
<input type="file" name="file_field" id="user_avatar">
<a href="#" class="fake">
  <img src="https://fthmb.tqn.com/65lNzIRNfZY4xY02D17b1RcGvso=/960x0/filters:no_upscale()/kitten-looking-at-camera-521981437-57d840213df78c583374be3b.jpg" width="320" height="240">
</a>
brk
  • 48,835
  • 10
  • 56
  • 78
2

You don't need JavaScript at all to solve this problem.

Just make the input invisible by setting its opacity:0 and position both elements absolutely within a common parent element, then make sure the input is on the top layer and is the same size as the image behind it.

#user_avatar { opacity:0; position:absolute; z-index:9; width:320px; height:240px; }
img { position:absolute; z-index:-1; }
<div>
  <input type="file" name="file_field" id="user_avatar">
  <img src="https://fthmb.tqn.com/65lNzIRNfZY4xY02D17b1RcGvso=/960x0/filters:no_upscale()/kitten-looking-at-camera-521981437-57d840213df78c583374be3b.jpg" width="320" height="240">
</div>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71