-2

I'm trying to print the relative URL from the href shown below in the console:

<div class="span-8 last">
  <span class="large">
    <a href="/property/3938749">1221 Pearl St.</a>
 </span>
</div>

using the following jQuery call:

$(document).ready(function(){
  $(".span-8 .large").hover(function(){
    var href = $(this).attr("href");
    console.log(href);
});

which I believe is essentially what was described here, but when I hover over the selected element, instead of

/property/3938749

the console is showing me

undefined

Any suggestions as to what I'm missing would be greatly appreciated!

Community
  • 1
  • 1
Michael
  • 13
  • 3
  • Possible duplicate of [How to change the href for a hyperlink using jQuery](http://stackoverflow.com/questions/179713/how-to-change-the-href-for-a-hyperlink-using-jquery) – ProllyGeek Oct 17 '15 at 04:21
  • @ProllyGeek Not a duplicate. This at best a typo-like question, in that the OP is attempting to get an attribute on the wrong element. – Daedalus Oct 17 '15 at 04:23
  • @Daedalus i was in hurry so made that from mobile , you are totally right though , so experience on mobile really sucks. – ProllyGeek Oct 17 '15 at 14:14
  • @Daedalus Doh, you're right. I'm _very_ new to jquery and I got confused about what element I was trying to select. – Michael Oct 17 '15 at 18:19

2 Answers2

4

$(document).ready(function() {
  $(".span-8 .large").hover(function() {
    var href = $(this).find("a").attr("href");
    console.log(href);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="span-8 last">
  <span class="large">
    <a href="/property/3938749">1221 Pearl St.</a>
 </span>
</div>
Omidam81
  • 1,887
  • 12
  • 20
2

$(document).ready(function() {
  $(".span-8 .large").hover(function() {
    var href = $(this).find('a').attr("href");
    console.log(href);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="span-8 last">
  <span class="large">
    <a href="/property/3938749">1221 Pearl St.</a>
 </span>
</div>

You need to get the anchor so select the anchor by .find('a')

guradio
  • 15,524
  • 4
  • 36
  • 57