Question: I have the following HTML structure, I want to add a class to the last `<div>` which contains the `<p>` tag but only when it exists. 

```
<div id="view">
  <div class="login">
  </div>
  <div>
    <p>Add a class to this parent DIV</p>
  </div>
</div>

```

Any suggestions?

Answer: 
Simple snippet:

```
if($('#view div:last p').​​​​​​​​​​​length)
    $('#view div:last').addClass('myClass');​​​​​​​​​​​

```
Yes Roaster given right answer and this is the fiddle for it. <http://jsfiddle.net/sameerast/qhsXM/>

```
<div id="view">
  <div class="login">
  </div>
  <div>
    <p>Add a class to this parent DIV</p>
  </div>
</div>​

if($('#view div:last p').length){
    $('#view div:last').addClass('myClass');
}
​
.myClass {
    border:red 1px solid;
}​

```