Tanabe Saori
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~~~~~~~
how do I call this, or simulate a click once the page is loaded?
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.
If that is not the required action you will need to isolate the button either with a parent element or with an id.
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.
and it's probably preferred that this is done after page load. Does that mean putting it at the end of the page?
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');
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');
});
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'm not too sure why you accepting your own comment as the answer.
but you didn't share your HTML element codes so we got no chance to debug for you.
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
If you have a button with id = 'jp' then all you need to do is
document.getElementById('jp').click();
If you want to remember which button a user clicked you can use localStorage
HTML
Working sample here
HTML
<button id="jp" class="translate">JP</button>
<button id="en" class="translate">EN</button>
<button id="reset">Reset</button>
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>
Working sample here
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('j p').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.
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('j
Thanks everyone for your efforts.
However document.getElementById('jShow us your full code so we can see the implementation.p').click( ); isn't working, even when I put it at the end of the page
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.
Open in new window