0

Here when I am clicking on More Info. I want an alert showing the value present inside h3 tags. How can I do that ??

Here is the html i am working on

<div class="col-lg-4 col-xs-6"> 
  <div class="small-box bg-red">
    <div class="inner">
      <h3>Text 1</h3>
      <p>fhjh</p>
    </div>

  <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
  </div>
</div>
Denis Tsoi
  • 9,428
  • 8
  • 37
  • 56
Prakhar
  • 530
  • 10
  • 24
  • Give it an id and jquery it, via the new id – depperm Jun 08 '15 at 03:35
  • similer questions http://stackoverflow.com/questions/9053983/alert-id-of-current-element , http://stackoverflow.com/questions/3239598/how-can-i-get-the-id-of-an-element-using-jquery – Malik Jun 08 '15 at 03:37

5 Answers5

1

so what you want is to click on the small-box-footer link and alert the 'Text 1'?

$(document).ready(function(){
    $('.small-box-footer').on('click', function(evt){
         //if you don't want the default event
         evt.preventDefault();
         //show the alert text
         alert($('.inner h3').text());
    })
})
Surely
  • 1,649
  • 16
  • 23
0

you can try :

$('.small-box-footer').click(function(e){
    var text = $(this).prev().children().first().text();
    console.log(text);
});
Yangguang
  • 1,785
  • 9
  • 10
0

you need to have an event listener for the onclick event when someone clicks on the "more info".

you'll then need to get the closest query the dom (you can traverse upwards or alternatively go from the DOM straight to a specific element... it's cheaper to go up).

example...

Using Jquery

$('.small-box-footer').click(function(ev) {
  ev.preventDefault();
  // find the h3
  var text = $('.inner h3').text();
  alert(text);
});
Denis Tsoi
  • 9,428
  • 8
  • 37
  • 56
0

Simple as just grabbing the h3 directly, skip traversing:

window.showTitle = function() {
  alert($('h3').text())
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-lg-4 col-xs-6">
  <div class="small-box bg-red">
    <div class="inner">
      <h3>Text 1</h3 
<p>fhjh</p>
</div>
<a href="#" class="small-box-footer" onclick="showTitle()">More info <i class="fa fa-arrow-circle-right"></i>
</a>
</div>
</div>
omikes
  • 8,064
  • 8
  • 37
  • 50
0

you can try code below

$(document).ready(function(){
    $('.small-box-footer').on('click', function(e){
         e.preventDefault();        
         alert($(this).parents('.small-box').find('h3').text());
    })
})

Working Demo

Vuthy Sok
  • 720
  • 3
  • 10
  • 22