user3653474
user3653474

Reputation: 3854

Finding the ul inside a div and append li tag to that ul using jquery

I want to find the ul element inside a div and append li tag inside that ul using jquery. As given below in the code a parent div with id=select_name_chosen,i want to find ul class="chosen-results and append li in that div using jquery. Please help to solve my issue. This is jquery code what i am using.

var parent = $("div#select_name_chosen");
var childUl = parent.children('.chosen-results');
childUl.append($("<li class='active-results' data-option-array-index='0'>name</li>"));



<div class="chosen-container chosen-container-multi chosen-with-drop chosen-container-active" style="width: 350px;" title="" id="select_name_chosen">
<ul class="chosen-choices">
<li class="search-field">
<input type="text" value="Select Subject" class="default" autocomplete="off" style="width: 110px;" tabindex="4">
</li>
</ul>
<div class="chosen-drop">
<ul class="chosen-results">   
</ul>
</div>
</div>

Upvotes: 1

Views: 4245

Answers (2)

Varun
Varun

Reputation: 1946

This should work,it appends the li elements to the ul of class='chosen-results'.

$("#select_name_chosen").find("ul[class='chosen-results']").append("<li class='active-results' data-option-array-index='0'>name</li>");

Fiddle: https://jsfiddle.net/fos2Lrab/

Upvotes: 2

Gary Storey
Gary Storey

Reputation: 1814

You have a couple of unnecessary calls to the jQuery object in your code.

var parent = $("#select_name_chosen");
var childUl = parent.find('.chosen-results');
childUl.append("<li class='active-results' data-option-array-index='0'>name</li>");

Here is a fiddle.

Upvotes: 0

Related Questions