Abhimanyu
Abhimanyu

Reputation: 4917

trigger an anchor tag using the .click() event

For my application, I want to trigger an anchor tag using the .click() event, which should redirect to page mention in href.

I'm using following code to achieve it.But its not working as expected.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title></title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    </head>
    <body>
    <script>
    $(function() {
        $('#upload').click(function(e) {
            e.preventDefault();
                $("#login_or_register").click();
        });
    });
    </script>
    <div id ='upload'>upload</div>
    <a  style ='display:none' href="abc.html" id="login_or_register">Login or register</a>
    </body>
    </html>

Help me out!!

Upvotes: 0

Views: 10787

Answers (1)

Nick Craver
Nick Craver

Reputation: 630399

You are clicking the link, but that won't cause the browser to follow the link (the "default action" or behavior is what this is called), so instead of this:

$("#login_or_register").click();

You need this:

window.location.href = 'abc.html';
//or dynamically:
window.location.href = $("#login_or_register").attr('href');

Upvotes: 10

Related Questions