Domanda

I want to select with jQuery the last element that it has pu class before the element with which I launch this event knowing that I there are a lots of elements before and after the current that have this class

Here is an example :

<div class="pu" ></div>
<div class="pu" ></div>
<div class="pu" ></div>
<div class="current" ondragenter="myfunction();" ></div>
<div class="pu" ></div>
<div class="pu" ></div>

for this example, I want to select the last element that has pu class before the div that has current class when the ondragenter is launched.

I have this case because the div are generated from server side

How can I achieve that?

Thanks!

È stato utile?

Soluzione

This will take care of selection even-though your immediate previous element to current does not have the pu class.

http://jsfiddle.net/2Vunz/

PrevAll

$('div.current').prevAll('div.pu:first');

This will take care of any wrappers.

http://jsfiddle.net/S2LfA/

$('input.current').parent().prevAll().find('input.pu').eq(0)

Altri suggerimenti

The following line of jQuery will select the previous sibling elements of div.current based on the selector div.pu:

$('div.current').prevAll('div.pu:first');

"Previous sibling elements" and that selector signify that this will get you the pu class div right before the current class div, assuming they are on the same level of the DOM like in your example.

try this code:

$('div.pu:last')
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top