Right way to get a particular div

1

I'm trying to figure out a way to access a div via jquery to manipulate it. Here is the code:

<div> <!-- Essa é a div que quero acessar -->
  <div>
    <div>
      <div id="conteudo">...</div>
    </div>
  </div>
</div> 

As shown in the comment above, the div I want to access is the one with the highest root shown here. I know these divs around are unnecessary, but sometimes some frameworks and cms put them, and then I find myself in a situation like that, having to access the div I want to move, as it is in the comment.

What I do in jquery to access is as follows:

$("#conteudo").parent().parent().parent().css('padding','15px');

How can I access this div without this whole code? I often do not get these unnecessary divs, so I wanted to access the divs I want in a more effective way (imagine if there were 10 divs there, how many parents I would have to write ...)

    
asked by anonymous 17.11.2015 / 13:07

2 answers

4

Combine the .parents() function with the :eq() :

$("#conteudo").parents(':eq(2)')

Note that the indexing is at 0, so to access the 3rd level it should be used.

    
17.11.2015 / 13:43
1

Place an identifier on it an ID or a class and call it directly

<div id="esse"> <!-- Essa é a div que quero acessar -->
  <div>
    <div>
      <div id="conteudo">...</div>
    </div>
  </div>
</div> 

Then make $('#esse').css('padding','15px'); if it is not possible to move in this div then the Sanction has already shown the other way.

    
17.11.2015 / 13:49