user2952265
user2952265

Reputation: 1630

jQuery - getting an element by class via parent

I have a wrapper div and inside a button and a hidden div. When pushing the button the div shall be displayed. I don't want to use ids because the construction apears several times on the webpage. What is the right jquery command?

js fiddle

HTML

<div class="wrap">

   <div class="button"> see more </div>
   <div class="moreInfos"> Lorem ipsum... </div>

</div>

CSS

.moreInfos{ display: none;}

jQuery

    $(".wrapp").on('click', '.button', function(){

    $(this).parent('.moreInfos').css('display', 'block');
});

Upvotes: 1

Views: 89

Answers (3)

Venkata Krishna
Venkata Krishna

Reputation: 15122

You were very close. Use parent().find()

$(this).parent().find('.moreInfos').css('display', 'block');

Also, you have a type $(".wrapp") extra 'p'.. just$(".wrap")`

You also have a typo here: not diplay, its display

jsFiddle DEMO

Upvotes: 4

BeNdErR
BeNdErR

Reputation: 17927

If the click event is bound to the button:

$(this).parent().find(".moreInfos").show();

if the click event is bound to the wrapping div:

$(this).find(".moreInfos").show();

FIDDLE http://jsfiddle.net/BeNdErR/acN6R/8/

Upvotes: 6

Karl-Andr&#233; Gagnon
Karl-Andr&#233; Gagnon

Reputation: 33880

Try using siblings:

$(".wrap").on('click', '.button', function(){

    $(this).siblings('.moreInfos').show();
});

Fiddle : http://jsfiddle.net/acN6R/5/

Upvotes: -1

Related Questions