Yogesh Malpani
Yogesh Malpani

Reputation: 971

how can i make an anchor tag to follow a link (href) and then make a click programatically on that link.

I am completely new to jquery and is it possible to make an anchor tag follow the href page and then produce a click event automatically on an element.(nextpageanchor)

for eg:

//Current page

<a href="nextpage.html" id="currentpageanchor">Click to go to nextpage</a> //on clicking manually v go to nextpg

//next page

<a href="#" id="nextpageanchor">I do some logic make me click programatically</a>

please help

Upvotes: 0

Views: 1653

Answers (4)

faino
faino

Reputation: 3224

Lets say you have an anchor link like so:

<a name="my_anchor" href="http://www.google.com"></a>

That's on another page; the link from the original would be something like:

<a href="another_page.html#my_anchor">Link to anchor on another page</a>

Then, to make the function run, a simple test() should work:

$(function(){
    if(/#my_anchor/i.test(window.location.hash)) {
        $('a[name="my_anchor"]').click();
    }
});

Upvotes: 1

Broncha
Broncha

Reputation: 3794

Well you cannot control the jquery event in the page that will be loaded when that anchor is followed directly. What you could do is like EvilP said, pass some flag to the server when the anchor is clicked, and when you render the other page, check for flag and if the flag is set then echo out some javascript triggering a click event on the page onload.

Upvotes: -1

OptimusCrime
OptimusCrime

Reputation: 14863

Not sure what you mean...but if this is what you want:

Click #nextpageanchor and go to the link in #currentpageanchor, this should do it.

$('#nextpageanchor').on('click',function () {
    $('#currentpageanchor').trigger('click');
});

Upvotes: -1

mas-designs
mas-designs

Reputation: 7546

On your current page you could add a parameter to your url like

<a href ="nextpage.html?cameFromA=true"></a>

And on your nextpage: you could look if the parameter is available and use $('something').trigger('click');

Upvotes: 2

Related Questions