Timmy Von Heiss Timmy Von Heiss - 3 months ago 11
jQuery Question

how to change window.location and then trigger an element?

$('.create-box').on('click', function() {
window.location.href = "/photos/new/";
$('input#photo_picture').focus().trigger('click');
});


I want to click on a button that will change the url and then trigger the input on the new page.

Answer

On first page use this code for changing the page url to /photos/new/:

$('.create-box').on('click', function() {
  window.location.href = "/photos/new/";
});

and on target page you can focus on input with this code:

$('input#photo_picture').focus();