Fredrik
Fredrik

Reputation: 635

Open link with jquery

I'm totally not good at jQuery, but this is getting hilarious. Searching the web for 2 hours, trying to found how to open a link with jQuery, without any result.

I have tried this example: http://jqueryui.com/demos/button/#default

What I want is from the example above, change this code (and get it to work):

<a href="#">An anchor</a>

to

<a href="my_site.php">An anchor</a>

How can I do this?

Upvotes: 1

Views: 2170

Answers (4)

Ken Browning
Ken Browning

Reputation: 29101

That page adds an event handler to the buttons' click events. That event handler returns false which is a technique for telling the browser "don't actually follow this link when someone clicks it." This prevents the link from behaving the way you expect.

Upvotes: 1

Fresheyeball
Fresheyeball

Reputation: 30035

$('a').attr('href', 'my_site.php');

however I advise you to give you link an id so it does not do this to ALL links, so

<a id="linkness" href="#" >An anchor</a>

$('#linkness').attr('href', 'my_site.php');

Upvotes: 1

FishBasketGordo
FishBasketGordo

Reputation: 23142

This should work:

$('a[href=#]').attr('href', 'my_site.php');

If you have more than one anchor tag on your page, you'll probably want a better selector than a[href=#], but you get the idea.

Here's a working example: http://jsfiddle.net/FishBasketGordo/WGPYp/

Upvotes: 0

genesis
genesis

Reputation: 50982

$("a[href='#']").attr('href', 'my_site.php');

Upvotes: 3

Related Questions