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
  • 351
  • 4
  • 15

3 Answers3

1

You can try this:

if($('ul.criteriallist li:first-child select').length == 0) {
//code to add dropdownlist
}
Milind Anantwar
  • 79,642
  • 23
  • 92
  • 120
1

You can use:

if($('ul.criteriallist li:eq(0) select[name="cao1"]').length === 0) {
    // add it
}
techfoobar
  • 63,712
  • 13
  • 108
  • 129
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,345
  • 11
  • 37
  • 58