0

I wish to add a select list under the first 'li' of my list.

It works with this method.

$("ul.criteriallist li:eq(0)").html("<select name='cao1' class='cao ops'></select>" 
+ $("ul.criteriallist li:eq(0)").html());

However, there's also another case where I would like to check whether the select list already exists within the 'li' item (to prevent adding duplicates). Do you have any recommendation on how I can do that?

What I'm thinking is something like.. (some pseudo code here..)

//If select list 'cao1' does not exist in this list item 

       // Add select list in this item 
kosherjellyfish
  • 361
  • 4
  • 16

3 Answers3

1

You can try this:

if($('ul.criteriallist li:first-child select').length == 0) {
//code to add dropdownlist
}
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1

You can use:

if($('ul.criteriallist li:eq(0) select[name="cao1"]').length === 0) {
    // add it
}
techfoobar
  • 65,616
  • 14
  • 114
  • 135
0

Try:

if(!(("ul.criteriallist li:eq(0)").find("select").length)){
    $("ul.criteriallist li:eq(0)").prepend("<select name='cao1' class='cao ops'></select>"); 
}
codingrose
  • 15,563
  • 11
  • 39
  • 58