1

I have a div "editableDiv" which is nested inside another div. editableDiv has an unordered list as its child item. Please go through the code

<div>
    <div id="editableDiv" contenteditable="true">
        <ul id="suggestUL"></ul>
    </div>
</div>

Question is, how to find the innermost ul suggestUL

Thanks in advance.

Basavaraj Metri
  • 806
  • 1
  • 13
  • 27

6 Answers6

5

You can get ul directly through id using id selector as id is supposed to be unique but you can use descendant selector as well

Using id selector

$('#suggestUL')

Use descendant selector

$('#editableDiv ul');
Adil
  • 143,427
  • 25
  • 201
  • 198
1

Also using .find()

$('#editableDiv').find('ul');
Praveen
  • 53,079
  • 32
  • 129
  • 156
1

1) you can find it by ID ( which should be unique).

$('#suggestUL');

2) assuming there is no ID and you want to find the innermost UL :

how to find the innermost ul

<div>
    <div id="editableDiv" contenteditable="true">
        <ul ></ul>
    </div>
</div>

Try this :

$('#editableDiv ul:last');
Royi Namir
  • 138,711
  • 129
  • 435
  • 755
1

Most proper way to find ul element inside of div with jQuery is as following:

$('div#editableDiv').find('ul')

Read about selector performance related information from here.

Community
  • 1
  • 1
Khamidulla
  • 2,899
  • 6
  • 32
  • 58
1

You can find out this by way, using div id .

$(document).ready(function(){
$("#editableDiv").find('ul');
    $("#editableDiv").find('ul').text("sss");
    console.log($("#editableDiv").find('ul'));
})

Demo

Zeeshan
  • 1,661
  • 12
  • 16
1

Because you have tagged this question with css I thought I would add an answer showing you that you can style it simply in css like this..

#suggestUL { // Your Styling } or #editableDiv ul { // Your Styling }

It depends what you want to do with the UL, your question of 'how to find the innermost ul' could be interpreted in a few ways.. perhaps you should consider improving your question?

Wez
  • 10,267
  • 5
  • 48
  • 63