0

I have a link in HTML thats structured like:

<a href="#!/example/1234" class="details">details</a>

When I target this a tag with $("a.details").attr('href') or $("a.details")[0].href both return only the # portion of the href and nothing past it.

I tried searching for what is valid and invalid characters for a href attribute in an '<a>' tag and didn't turn up anything really useful. I know that # denotes it being an anchor tag but why would this block it from jquery reading the value?

Any help would be appreciated!

Stefan Woskowiak
  • 115
  • 1
  • 1
  • 6

2 Answers2

3

Works fine for me here:

http://jsfiddle.net/Hz9uX/

Are you putting quotation marks around your a.details?

$('a.details').attr('href')

switz
  • 24,384
  • 25
  • 76
  • 101
  • yeah sorry I have the selector formatted correctly $('a.details').attr('href'). Still giving me just getting #. When the href was just href="1234" i was able to get number just fine but once the whole #!/product/1234 was added it stopped reading it properly. – Stefan Woskowiak Nov 07 '11 at 18:51
  • Did you try my fiddle? What appears in the alert? Also, what version of jQuery are you using? – switz Nov 07 '11 at 18:51
  • Yeah I do see it working in your example. I just made sure it wasn't an issue of timing since some of this stuff is dynamically added but even the timeout happening well after the fact logs out a '#' :( – Stefan Woskowiak Nov 07 '11 at 19:00
  • It probably either has something to do with you dynamically adding it after it checks the anchor tag or you're using an old version of jQuery. I'd put my money on the former. Without more code, we can't help you. – switz Nov 07 '11 at 19:42
0

Try (demo)

$('a').attr('href');
Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
xkeshav
  • 53,360
  • 44
  • 177
  • 245