ryan
ryan

Reputation:

Get all <a> elements in a page using javascript

Is there anyway I can get all of the <a> in a page, I want to apply a new targets to all of them.

Upvotes: 3

Views: 441

Answers (5)

Guilherme
Guilherme

Reputation: 561

Using purely Javascript DOM:

var links = document.getElementsByTagName("a");

You can check out reference at: http://www.javascriptkit.com/domref/elementmethods.shtml

Upvotes: 0

John MacIntyre
John MacIntyre

Reputation: 13031

I believe this will do what you need:

document.getElementsByTagName("A");

Upvotes: 0

Greg
Greg

Reputation: 321776

You can use

document.links

or in jQuery

$('a')

or in DOM

document.getElementsByTagName('a')

Upvotes: 12

Jason
Jason

Reputation: 52527

if you're using jQuery:

$("a").attr("target","_blank")

Upvotes: 1

Justin
Justin

Reputation: 9801

If I recall correctly, the prototype library has something like this...

Upvotes: -2

Related Questions