Grabbing attribute or href on a click event

I am trying to capture the href when a button is clicked, but not having much luck.

Here is the html

<div class="col-sm-12 hbtn-1">
  <a href="http://atlanticbay.com/blog/finances/hard-vs-soft-pull-on-credit/" 
  class="btn-ghost-white " onclick="ga('send', 'event', 'Header Button', 'click',
  'Read Article -http://atlanticbay.com/blog/finances/hard-vs-soft-pull-on-credit/);"
   >Read Article</a>
</div>

here is what I tried so far.

$('.btn-ghost-white').click(function() {
 var blogVideo = $(this).attr('href');
  
  if (blogVideo.length) {
    // Do something here
    s.eVar38 = blogVideo;   
    return true;
  } else {
    // Don't follow the link
    return false;
  }
});

and I tried this

$(‘.col-sm-12 hbtn-1 a').click(function() {
  var blogvideo = $(this).children(‘a’);
  
  if (blogvideo.length) {
    // Do something here
  s.eVar38 = (.attr(‘href’) );  
  } else {
    // Don't follow the link
    return false;
  }
});

Would appreciate your help.

That first code does work but you’re missing a ’ at the end of the onclick attribute in the html.

Here’s the complete code that runs as I’d expect.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
</head>
<body>
<div class="col-sm-12 hbtn-1">
  <a href="http://atlanticbay.com/blog/finances/hard-vs-soft-pull-on-credit/"
  class="btn-ghost-white " onclick="ga('send', 'event', 'Header Button', 'click',
  'Read Article -http://atlanticbay.com/blog/finances/hard-vs-soft-pull-on-credit/');"
   >Read Article</a>
</div>
<script
  src="https://code.jquery.com/jquery-3.1.0.min.js"
  integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="
  crossorigin="anonymous"></script>
<script type="text/javascript">

// as we don't have google analytics
window.ga = function() {};

$('.btn-ghost-white').click(function() {
  var href = $(this).attr('href');

  if (href) {
    console.log(href)
    return false
  }
  else {
    // Don't follow the link
    return false;
  }
});
</script>
</body>
</html>

Speaking of quotes

If your editor is changing quotes from '' to ‘’
Please either reconfigure the editor or use a different one

The fancy curly quotes may look great for things like a printed party invitation, but they will break your code.

1 Like

Thanks

Ok so on that same note… I tried doing the same for the following, but I am missing something. Would appreciate your help.

the html is the following… this is just a snippet, but is the code that is the cocern…

<div id="hero-intro" class="col-sm-7 col-sm-offset-1">
                <h1 id="mb-name" data-mb-name="Ann Arnesen">Ann Arnesen</h1>
                <h3 id="hero-Title"><span>Sr. Mortgage Banker</span> | <span>NMLS #453374</span></h3>
                <!-- #hero-title -->
                <div class="sep-short-blue"></div>
                
                                <h5 id="hero-rating">Overall Satisfaction Rating:
                    <span class="rs rating-stars"><i class="fa fa-star fa-star-full"></i> <i class="fa fa-star fa-star-full"></i> <i class="fa fa-star fa-star-full"></i> <i class="fa fa-star fa-star-full"></i> <i class="fa fa-star fa-star-full"></i></span> <span class="rs rating-percent">5 out of 5</span>                </h5>
                                <!-- #hero-rating -->
                
                <p id="hero-intro-text">With over 10 years in the mortgage industry, Ann Arnesen has helped over 1,100 families achieve their dream of owning a home. The majority of Ann’s clients are repeat customers.</p>
                <!-- #hero-intro-text -->
                
                <div id="hero-readmore"><a href="#biography" data-toggle="modal">Read full bio...</a></div>
                <!-- #hero-readmore -->
                
                <div id="hero-btn-group"> 
                <a class="btn-ghost-white btn-left page-scroll" href="#social-c">CONTACT ME</a>
                                    <a class="btn-ghost-white btn-right" href="https://www.secureloandocs.com/apply.php?id=36309094&amp;loId=16167" target="_blank">APPLY ONLINE</a>
                                </div>
                <!-- #hero-btn-group --> 
            </div>

this is the JQuery script I wrote, but is not working correctly. Basically on the click event I want to capture the data-mb-name attribute → in this case it would be Ann Arnensen.

$('.hero-btn-group a').click(function() {
  var getElement = $(this).parents('Div:has(#hero-btn-group'));
  
  if (getElement.length) {
    // Do something here
  s.eVar52 = ( getElement.attr('data-mb-name')); 
   console.log(s.eVar52); 
 
  } else {
    // Don't follow the link
    return false;
  }
});

Not sure what I am doing wrong.

The quote is on the wrong side of the parentheses. ^^

Thanks. @m3g4p0p . You have consistently been such a great help. I really appreciate you.

1 Like

Sorry everyone but I submitted this to the wrong subject. I will resubmit it in the correct forum/subject.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.