CodyBugstein
CodyBugstein

Reputation: 23342

Using AngularJS Jqlite, how can I find the first child of an element that is of a certain element type?

In my AngularJS directive, I want to apply focus to the first child of type <pre> of the last child of type <div>.

That is if my document looks like this:

<div main-div>
     <div>
     </div>
     <div>
          <div>
               <pre></pre>
          </div>
     </div>
     <div>
          <div>
               <pre the-one></pre>
          </div>
     </div>

the <pre> it should select is the last one, with an attribute the-one.

Is there any way to do this?

Upvotes: 2

Views: 6320

Answers (1)

New Dev
New Dev

Reputation: 49590

if you have the structure above as a jqLite element (however you get it a hold of it), you could do the following:

var divs = element.children().find("div");
var theOne;
if (divs.length > 0){
  theOne = angular.element(divs[divs.length-1]).find("pre");
}

Upvotes: 4

Related Questions