hitzi
hitzi

Reputation: 852

How can I find a root div with jQuery?

I use the jQuery function find() to extract a div of a html file. I use it in that way

data.find('#tpl_header')

Problem is jquery find() find only non root elements. So this wont work:

[...]
<body>
   <div id="tpl_header" class="table header">
      <div class="tr">
      </div>
   </div>
</body>
</html>

But this way works:

[...]
<body>
   <div id="template"> <!-- because jQuery find function did not find root elements! -->

       <div id="tpl_header" class="table header">
          <div class="tr">
          </div>
       </div>
   </div>
</body>
</html>

Is there a way to find this template div without adding a additional not really needed div?

[ADD]

The template reading function - already with the changes mention below by Sjoerd:

function LoadTemplate()
        {
            $.get('templates/' + template + '/main.html',  
                function(data) {
                    data = $(data);
                    $('#header').html($('#tpl_header', data));
            });
        }

Upvotes: 1

Views: 2743

Answers (2)

hitzi
hitzi

Reputation: 852

Another thread gives me a working solution. I have to use the .filter() function to get the root div.

Source: how can get attributes of root element?

Upvotes: 0

Sjoerd
Sjoerd

Reputation: 75629

var templateElement = $('#tpl_header')

element.find() only finds descendants of that element, whereas $() finds elements on the whole page.

Upvotes: 5

Related Questions