Freddy
Freddy

Reputation: 867

Targeting parent div (one level above child with class)

I have the following markup:

<div>
  <div class="richtext">
    dummy text
  </div>
</div>

I'm looking to use JQuery to achieve the following:

<div style="width: 100%;">
  <div class="richtext">
    dummy text
  </div>
</div>

To do this, I've done the following:

$(".richtext").prev('div').css('width','100%');

But it doesn't add the inline style to the div?

Upvotes: 1

Views: 52

Answers (4)

Sascha A.
Sascha A.

Reputation: 4626

$(".richtext").parent().css('background-color', 'yellow');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div> Start parent DIV
  <div class="richtext">
    dummy text dummy text dummy text dummy text
  </div>
  End parent DIV
</div>

Upvotes: 1

hgb123
hgb123

Reputation: 14891

You could iterated through the selected

$('div.richtext').each(function() {
  $(this).parent().css("color", "red");
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <div class="richtext">
    dummy text
  </div>
</div>

Upvotes: 0

Lalji Tadhani
Lalji Tadhani

Reputation: 14159

try with .parent()

$(".richtext").parent().css('width','100%');

https://jsfiddle.net/lalji1051/m5wf7ur9/2/

Upvotes: 1

Dan Mullin
Dan Mullin

Reputation: 4415

Use .parent() to get the parent.

$(".richtext").parent().css('width','100%');

Upvotes: 1

Related Questions