Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select previous element using jQuery selector only

I need to select the previous element using only a jQuery selector.

<li class="collapsable">
    <div class="hitarea collapsable-hitarea"></div>
    <span id="devicelist" class="ankr">Device List</span>
    <ul id="ultree">
        <li type="dev" device="/dev/sdh1" id="dev0" class="litab">
            <img height="20px" width="20px" src="../Images/drive.png" class="dicon">
            <a class="ankr dn" href="#">'/dev/sdh1'</a>
        </li>
    </ul>
</li>

$(document).on("click","#devicelist,**Here Selector will be used**",this,function(event){
    console.log(this);
    console.log(event.target);
});

I want to select div which has class .hitarea. I am looking for something like $("#devicelist:div.hitarea")

It should be done only with a selector of jQuery.

like image 522
Rickyrock Avatar asked Nov 12 '22 05:11

Rickyrock


1 Answers

you can use the prev() selector

Description: Get the immediately preceding sibling of each element in the set of matched elements, optionally filtered by a selector.

$('#devicelist').prev();
like image 66
MaVRoSCy Avatar answered Dec 06 '22 16:12

MaVRoSCy