http203
http203

Reputation: 851

How to find the first instance of element1 or element2 using JQuery?

How would I go about getting the first instance of an INS element OR the first instance of a DEL element depending on which is displayed first?

Using jQuery, I can get the first of each element in a div using this:

$("#output").find("ins").first();
$("#output").find("del").first();

In the following html:

<div id="output">
   This is some text with some <ins>added text</ins> or maybe some <del>deleted text</del>
</div>

I'd like to get the INS element because it appears first.

But in this version:

<div id="output">
   This is some <del>deleted text</del> and also some <ins>added text</ins>.
</div>

I'd like to get the DEL element because it appears first.

Any thoughts?

Upvotes: 0

Views: 93

Answers (3)

Twisha Desai
Twisha Desai

Reputation: 41

$("#output1 > ins,#output1 del").first()

http://jsfiddle.net/a6dLH/

Upvotes: 0

Jamie Barker
Jamie Barker

Reputation: 8246

If it's always the first element within #output, then you could do:

$('#output > *').eq(0)

http://jsfiddle.net/6Tevh/

Upvotes: 0

ncksllvn
ncksllvn

Reputation: 5769

$("#output").find("ins,del").first();

Upvotes: 6

Related Questions