Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Get tag inside element with class

Tags:

jquery

<div id="yes">
  <div class="wrapper">
    <something>...</else>
    <p>foobar</p>
  </div>
</div>

I want to get the p-tag to append a classname

$("div.wrapper", "#yes").addClass("newClassName");

But where to add the "p"?

like image 685
Martin Avatar asked Dec 17 '22 09:12

Martin


2 Answers

Try

$("div#yes > div.wrapper > p").addClass("newClassName");

Here is a jsFiddle demo

like image 78
Rusty Fausak Avatar answered Jan 09 '23 18:01

Rusty Fausak


This can be achieved by the folowing code:

$("#yes div.wrapper p").addClass("newClassName");

This code gets any p-tag within any div with the class "wrapper" which is located in an element with the id "yes".

like image 30
georg87 Avatar answered Jan 09 '23 18:01

georg87