KateYoak
KateYoak

Reputation: 1731

Getting href attribute as written with javascript

I would like to get an href attribute as it appears in the html - instead of a fully qualified url.

<a href="foo.html">foo</a>

$('a[href]').each(function(){
   alert(this.href);
});

Gets an absolute url starting with http:// . I'd like it to get "foo.html" instead.

Upvotes: 0

Views: 123

Answers (2)

George Cummins
George Cummins

Reputation: 28936

You will need to alter the way you loop through the element, and the way you use this:

$('a').each(function(){
   alert( $( this ).attr( 'href' ) );
});

Working example here.

Upvotes: 1

meder omuraliev
meder omuraliev

Reputation: 186672

Try .attr('href') to get the href value.

Upvotes: 2

Related Questions