Developer
Developer

Reputation: 1040

Get height of a parent div

  <div class='contain'>
     <a href="#">
        <div id="s1" class="sub" onclick="hello('1')">dasdad</div>
     </a>
  </div>
 <div class='contain'>
     <a href="#">
        <div id="s2" class="sub" onclick="hello('2')">dasdad</div>
     </a>
  </div>

From the above how can I get the height of the parent contain div when div sub is clicked in jquery.

Tried using $('#s1').parent().height();.

CSS

   .contain{margin:auto;width:auto; height:auto;}
   .sub{width:160px;min-height: 173px;}

Upvotes: 2

Views: 11448

Answers (4)

demonofthemist
demonofthemist

Reputation: 4199

Use

$('.sub').closest('.contain').height();

Use this JS

$('.sub').click(function(e){
    alert($(this).closest('.contain').height())
});

Upvotes: 0

Awlad Liton
Awlad Liton

Reputation: 9351

you have anchor as the parent of #s1 . so you need to use parent().parent()

try like this:

$('#s1').parent().parent('div.contain').height();

if you want to use class selector then try like this:

$('.sub').parent().parent('div.contain').height();

fiddle demo using iddemo using class

Upvotes: 0

Rob Schmuecker
Rob Schmuecker

Reputation: 8954

Whilst I concur with Anton's original answer,

I think a more complete example incorporating your original code would be beneficial.

Here is the demo http://jsfiddle.net/AjqJr/2/

Javascript:

$(document).ready(function () {
    window.hello = function (someNumber) {
        // Do something here
        console.log(someNumber);
    }

    $('.sub').on('click', function () {
        alert($(this).closest('.contain').height());
    });
});

Upvotes: 0

Anton
Anton

Reputation: 32581

Use .closest() just using parent will only get one level up the <a> element, so you must either use parent() twice or use closest()

$('#s1').closest('.contain').height();

DEMO

Upvotes: 7

Related Questions