Avatar of Tanabe Saori
Tanabe Saori
Flag 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?
JavaScript

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Ryan Chong

simply adding this:

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

Open in new window

Julian Hansen

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.
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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
Ryan Chong

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
Tanabe Saori

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ryan Chong

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.
Julian Hansen

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

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
Tanabe Saori

ASKER
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.
Julian Hansen

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy