Question: I am trying to select one ore more elements that are NOT descendants of another specific element.

```
<html>
<body>
    <div>
        <p>
            <b>
                <i> don't select me </i>
            </b>
        </p>
    </div>  
    <div> 
        <i>don't select me either </i>
    </div>  
    <i> select me </i>
    <b> 
        <i> select me too </i>
    </b>
</body>
</html>

```

In the example above I want to select all 'i' elements, that are *not* inside div elements.
The other way around would be easy, with ('div i'), but using this in :not() is not possible.

How can I select all i elements outside of div elements?
Often it is suggested the use of jQuery, which would be like: 

```
nondiv_i = all_i.not(all_div.find("i")) 

```

I can't use jQuery, but could use jqLite - jqLite does not have a not()-function. A jqLite solution is welcome too!
Is it possible to do this without repeated iterations and comparisons?

Edit: To clarify, i don't want to have any div-ancestors for my i-elements, not only no direct div-parents.

A comparable XPath would look like this:

```
//i[not(ancestor::div)]

```

Answer: 
```
function isDescendant(parent, child) {
    var all = parent.getElementsByTagName(child.tagName);
    for (var i = -1, l = all.length; ++i < l;) {
        if(all[i]==child){
            return true;
        }
    }
    return false;
}

```

for the specific case of yours;

```
is = document.getElementsByTagName("i");
divs = document.getElementsByTagName("div");
nondivs = [];
var contains;
for(i=0;i<is.length;i++){
    contains = false;
    for(j=0;j<divs.length;j++){
        if(isDescendant(divs[j],is[i])){
            contains = true;
            j = divs.length;
        }

    }
    if(!contains){
        nondivs.push(is[i]);
    }

}

```
Add a class to all of the `<i>` tags ("itag", for example). From there, you can fetch them by calling `getElementsByClassName`:

```
var r = document.getElementsByClassName("itag");
console.log("length" + r.length);

```

You can then get them by index:

```
console.log(r[0]);
console.log(r[1].innerHTML); // get text of i tag

```