Capturing URL that is inside a div with Jquery

0

I have a project where you have several boxes, with an image, and a link. I would like that when I click on the .single-project class I would capture the href value, but I am not getting it.

$('.single-project').on('click', function() {
  window.location.href = ($(this).attr("href"));
});
<div class="single-project">
  <img src="img/project/projeto01.jpg" alt="">
  <div class="project-overly">
    <a href="single-project.html">
      <h3>Titulo</h3>
    </a>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
  </div>
</div>
    
asked by anonymous 30.05.2018 / 14:53

2 answers

1

From what I understand you need the following code:

$('.single-project').on('click', function() {  
   window.location.href = $(this).find("a").attr("href");
});

If you have multiple URLs

$('.single-project').on('click', function() {
  var arr = [];
  $.each($(this).find('a'), function(index,valor){
   arr.push($(valor).attr('href'))
  });
  $('p').text(arr)
});
    
30.05.2018 / 15:01
0

Your problem consists of the parent level of the div and the this operator. Place the 'single-project' class in the <a> tag from which you want to extract the URL and where the click is triggered.

 $(document).ready(function() {
     $('.single-project').click(function(){
     	  alert($(this).attr("href"));
     })
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="outra-class">
          <img src="img/project/projeto01.jpg" alt="">
          <div class="project-overly">
            <a class="single-project" href="www.google.com.br">
              <h3>Titulo</h3>
            </a>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
          </div>
        </div>
    
30.05.2018 / 15:07