1

I have some HTML like this:

<span class="fetchSeries"><a href="sma10">10</a></span>

I want to get the value "sma10," but the href attribute becomes an absolute path when I try to reference it. Is there a way to get the relative path of the anchor tag?

Here's what I'm trying to accomplish:

  1. Bind a custom function to click() for any hyperlink within a span class="fetchSeries" tag.
  2. Extract a value that is unique for that link (eg. sma10).
Nate Reed
  • 6,761
  • 12
  • 53
  • 67

4 Answers4

3

If the unique value isn't really a href, you might use data attributes:

HTML

<span class="fetchSeries"><a href="#" data-myvalue="sma10">10</a></span>

JavaSCript

$('.fetchSeries a').click(function() {
   var myval = $(this).data('myvalue');
});

Docs: http://api.jquery.com/data

sje397
  • 41,293
  • 8
  • 87
  • 103
1

Please refer my answer this stackoverflow post: Jquery how to trigger click event on href element

Or using pure javascript

var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";

parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port;     // => "3000"
parser.pathname; // => "/pathname/"
parser.search;   // => "?search=test"
parser.hash;     // => "#hash"
parser.host;     // => "example.com:3000"

from https://gist.github.com/ldong/9735bcd2d3eca8c1038b

Community
  • 1
  • 1
Alan Dong
  • 3,981
  • 38
  • 35
1

Use thic code:

$(function(){
    var v = $('.fetchSeries a').attr('href');
    alert(v);
});
gor
  • 11,498
  • 5
  • 36
  • 42
  • I will try this. The problem is that when I was looking at "href" in firebug it was showing the absolute url, which is not what I want. – Nate Reed Jan 31 '11 at 16:02
0

You could use getAttribute to get the relative value of the href as long as the value of the href is relative like in the example.

i.e. document.getElementByID("theLink").getAttribute("href");

Saint48198
  • 121
  • 2