Click link after click another link - javascript

There is a way to simulate a click on a link after having clicked another?
I have made this script:
$(".uno").click(function() {
$(".due")[0].click();
});
Fiddle: https://jsfiddle.net/5ad3m8La/
When I click on "one" want "two" open the page as indicated.
My script does not work, and I do not understand why.

Update your fiddle. Updated one https://jsfiddle.net/5ad3m8La/2/
Made few changes given below:
1. Added target="blank" in link.
2. Added http:// before google's link.
3. Wrapped your code in document.ready.
and it's working now :)

$(".uno").click(function(e) {
e.preventDefault();
location.href = $('.due').attr('href');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
One
Two
This is not the same as emulating a click, but this way the behaviour is the same as clicking another linke (without special code attached to it)

Try the code below:
$('.due').trigger('click');

Try this:
One
Two
jQuery:
$( ".uno" ).click(function() {
$('.due').trigger("click");
});
$( ".due" ).click(function() {
var link= $(this).attr("href"); // you need to ger href and redirect to that link
window.location.href = link;
});

Related

How to automatically click a href link when the page loads?

I'm trying to automatically click a href link when the page loads.
<div class="loadmore" kind="rb">
تحميل المزيد...
</div>
The link is supposed to load more comments when you click it.
I tried this:
window.onload = function() {
autoloadmore()
};
function autoloadmore() {
var loadmoreClass = document.getElementsByClassName("loadmore")[0];
var loadmoreChild = loadmoreClass.getElementsByTagName("a");
if(loadmoreClass){
loadmoreChild[0].click();
}
}
but it doesn't seem to work. The problem seems to be with click(), because when I use other codes such as .style.color, they work.
I'm using Blogger by the way.
This's a sample blog:
http://blog-sample-001.blogspot.com/2018/09/title.html
(go to تحميل المزيد...)
ps: I can't change the div.
Assign some ID to تحميل المزيد... . For example, clickAfterPageLoad or whatever you want, and then try this...
$(document).ready(function() {
$("#clickAfterPageLoad").trigger('click');
});
or if you cant't change div, you can do like this:
$(document).ready(function() {
$(".loadmore a").trigger('click');
});

How do I get forwarded to a link that is in a data attribute in html when clicked using jQuery?

I am trying to forward to a link when the div is clicked using a data attribute and jQuery, but am stuck a bit. Here is the code I have:
html:
<div class="clickable-icon" data-link="www.google.com">click to be forwarded</div>
jquery:
$('.clickable-icon').attr('data-link');
used google as an example for the link to go to. But how would I use jquery to help with this?
you can do:
window.location.href = $('.clickable-icon').data('link');
and as you want on click, write click event and get clicked element data-link attribute value:
$('.clickable-icon').click(function () {
window.location.href = $(this).data('link');
})
WORKING FIDDLE
You could use window.location.href like
$('.clickable-icon').on('click', function() {
window.location.href = $(this).data('link');
});
$('.clickable-icon').click(function(){
window.location.href = 'http://'+$(this).data('link');
})

Why am I getting an undefined return?

EDIT: Solved
I am building a website in wordpress and am currently editing a jquery plugin to return the clicked link's url in an alert box but it is returning undefined. This is only suppose to be trigger when clicking on one of the items in the navigation menu(which that part is working fine) see website link below for reference. Why is this happening?
snippet of code:
jQuery('body').on('click', '.start-pgm li', function() {
var addressValue = $(this).attr("href");
alert(addressValue );
$.window({ title: "Cyclops Studio", url: addressValue });
return false;
});
Additional information if it helps:
Web Address I am working on
Jsfiddle with whole script
shouldnt it be
.on('click', '.start-pgm li a', function()
You are registering a click listener on a <li> element which does not have a href attribute.
.start-pgm li

simulating a click on a <a>-element in javascript

for a website, i am using the jQuery supzersized gallery script: http://buildinternet.com/project/supersized/slideshow/3.2/demo.html
As you can see in the demo, in the bottom right corner there is an little arrow button that toggles a thumbnail bar. There is no option in the config files to automatically blend this in when opening the site.
So i guess i have to simulate a click on that button (the button is the tray-button, see HTML). I tried something like this:
<script>
$(function() {
$('#tray-button').click();
});
</script>
However, this doesnt seem to work in any browsers i tested.
Any idea?
$('#tray-arrow').click(function() {
// prepare an action here, maybe say goodbye.
//
// if #tray-arrow is button or link <a href=...>
// you can allow or disallow going to the link:
// return true; // accept action
// return false; // disallow
});
$('#tray-arrow').trigger('click'); // this is a simulation of click
Try this
$("#tray-arrow").live("click", function () {
// do something
});
I assume that you want to popup the thumbnail bar #thump-tray on page load.
Here's a way to do it:
locate the file supersized.shutter.js and find this code:
// Thumbnail Tray Toggle
$(vars.tray_button).toggle(function(){
$(vars.thumb_tray).stop().animate({bottom : 0, avoidTransforms : true}, 300 );
if ($(vars.tray_arrow).attr('src')) $(vars.tray_arrow).attr("src", vars.image_path + "button-tray-down.png");
return false;
}, function() {
$(vars.thumb_tray).stop().animate({bottom : -$(vars.thumb_tray).height(), avoidTransforms : true}, 300 );
if ($(vars.tray_arrow).attr('src')) $(vars.tray_arrow).attr("src", vars.image_path + "button-tray-up.png");
return false;
});
After it, add:
$(vars.tray_button).click();
Dont forget in your page (demo.html in the plugin), to change
<script type="text/javascript" src="theme/supersized.shutter.min.js"></script>
to
<script type="text/javascript" src="theme/supersized.shutter.js"></script>
instead of using
$(function(){
//jquery magic magic
});
you culd try this witch will work your jquery magic after the full page is loaded (images etc)
$(window).load(function () {
// jquery magic
});
and to simulate a click you culd use // shuld be the same as $('#tray-arrow').click();
$('#tray-arrow').trigger('click',function(){ })
example:
$(window).load(function () {
$('#tray-arrow').trigger('click',function(){
alert('just been clicked!');
})
});
try
<script>
$(function() {
$('#tray-arrow').click();
});
</script>
Make sure that this code is after your carousel is initialized.
This looks like it's a problem of timing the trigger. The plugin also loads on document load, so maybe when you try to bind the event listener the element is not created yet.
Maybe you need to add the listener in something like the theme._init function
http://buildinternet.com/project/supersized/docs.html#theme-init
or somewhere similar.
A problem might be that your plugin detects whether the click has been initiated by a user (real mouse click), or through code (by using $('#id').click() method). If so, it's natural that you can't get any result from clicking the anchor element through code.
Check the source code of your plugin.

Looking to automate a webpage with links using Javascript

I'm new to programming and have a question related to HTML and Javascript. I have a HTML page with 100 links on it. I'm looking for a way wherein if I click on the first link then the rest of the links are clicked after it without me having to click them manually. Is there a way to do it?
Please help!!!
$("a:first").click(function() {
$("a:not(:first)").click();
});
Sample working code: http://jsfiddle.net/EJY8s/
$('a').each(function() {
$(this).click();
});
For full-syntax head to Jquery site documentation.
The following bit of Javascript will make it so you can click any one of those 100 links and open them each in a new window. 100 impressions, how nice.
$a = $("a");
$a.click(function(e){
e.preventDefault();
$a.each(function(){ window.open(this.href); });
});
Demo: jsfiddle.net/JB2YF
If you only want the first <a> to open all links, it's an easy tweak:
$("a:first").click(function(e){
e.preventDefault();
$("a").each(function(){ window.open(this.href); });
});

Categories

Resources