user1404602
user1404602

Reputation: 55

Jquery anchor link Event

i have a html like this

<a href="link.php" onClick="return false;">Click me</a>

$('a').click(function() {
var linked = "#"+$(this) .attr(href);
window.location=linked;
})

why this not working?

Upvotes: 0

Views: 150

Answers (4)

Claudio Redi
Claudio Redi

Reputation: 68400

Change

var linked = "#"+$(this) .attr(href);

by

var linked = "#"+$(this).attr("href");

EDIT

Try with this

$('a').click(function(e) {
    e.preventDefault();
    var linked = "#"+$(this).prop("href");
    window.location.hash = linked;
});​

Upvotes: 2

bukart
bukart

Reputation: 4906

$('a').click( function( event ) {
    location.href = '#' + $(this).attr( 'href' );
    event.preventDefault();
    event.stopPropagation(); // sometimes useful
    return false; // sometimes useful
} );

you should supress the triggered event

Upvotes: 0

andrux
andrux

Reputation: 2922

I would get rid of the onClick="return false;" part first and see what happens

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359776

If I understand what you're trying to do, you don't need JavaScript at all. Just change the href:

<a href="#link.php">Click me</a>

Upvotes: 1

Related Questions