Link to home
Start Free TrialLog in
Avatar of Tanabe Saori
Tanabe SaoriFlag for Japan

asked on

Simulate a click using javascript once page is loaded

I have a script that watches for a button click on class "translate". Each of these buttons has an id: jp or en:
$(document).ready(function(){
      $('.translate').click(function() {
        var lang = $(this).attr('id');
~~~~~~~snip~~~~~~~

Open in new window

how do I call this, or simulate a click once the page is loaded?
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

simply adding this:

$('.translate').trigger('click');

Open in new window

Note if you have more than one button on the page with that class your code will click all of them.

If that is not the required action you will need to isolate the button either with a parent element or with an id.
Avatar of Tanabe Saori

ASKER

Yes, so in the code I posted you can see that there is an id check.
and so, yeah, how do I do that?
(isolate the button).
and, again, here is the code snippet of the part that 'watches' for one of the 'languages' button press.
var lang = $(this).attr('id');

Open in new window


and it's probably preferred that this is done after page load.  Does that mean putting it at the end of the page?
and it's probably preferred that this is done after page load.  Does that mean putting it at the end of the page?

you can do it like this:

$(document).ready(function(){
      $('.translate').click(function() {
        var lang = $(this).attr('id');
        alert(lang);
      });

      $('.translate').trigger('click');
});

Open in new window


again, here is the code snippet of the part that 'watches' for one of the 'languages' button press.

are the buttons come with same id or different ids?
ASKER CERTIFIED SOLUTION
Avatar of Tanabe Saori
Tanabe Saori
Flag of Japan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm not too sure why you accepting your own comment as the answer.

var lang = 'jp';
which didn't work.

but you didn't share your HTML element codes so we got no chance to debug for you.
Question - on page load how do you know which button you want to click?

If you have a button with id = 'jp' then all you need to do is

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

Open in new window

If you want to remember which button a user clicked you can use localStorage
HTML
<button id="jp" class="translate">JP</button>
<button id="en" class="translate">EN</button>
<button id="reset">Reset</button>

Open in new window

JavaScript
<script>
$(function() {
	$('#reset').click(function(e) {
		e.preventDefault();
		localStorage.removeItem('translateBtn');
	});
	
	$('.translate').click(function() {
		alert('Button ' + this.id + ' clicked!');
	   localStorage.setItem('translateBtn', this.id);
	});

	var btnId = localStorage.getItem('translateBtn');
	if (btnId) {
	   document.getElementById(btnId).click();
	}	
});
</script>

Open in new window


Working sample here
Sorry Ryan Chong, but your responses didn't address what was needed, despite me providing enough information.
Your last response was to use [ var lang = $(this).attr('id');] when there wouldn't be any 'this' on a page load.

Julian's solution came after I had just decided to duplicate the function and hardwire the language.  he guessed that JP would be the one I'd want to open the site with first.
However document.getElementById('jp').click(); isn't working, even when I put it at the end of the page.  I thought about trying body onload, but directly running a copy of the function loads up the language first and probably renders the page sooner.  It's ugly, but it works.

Thanks everyone for your efforts.
However document.getElementById('jp').click(); isn't working, even when I put it at the end of the page
Show us your full code so we can see the implementation.

The sample I provided demonstrates the concept correctly - if it is not working for you then it is down to implementation. If we can see what you have done then we can tell you why your implementation is not working.