2

I want danamically create select element in horizontal block. And have funny result. What is the correct way?

JSFiddle

Gulaev Valentin
  • 575
  • 1
  • 7
  • 19

4 Answers4

2

You need to define where you want to append the new elements. For this, use .after(). And then you need to apply styles on the generated/modified controlgroup.

Demo here: http://jsfiddle.net/Palestinian/rKGtQ/

Code:

// append select menu after Button div
$("#Button_show_filters").after('<select id="sm"><option>Opt 1</option><option>Opt 2</option></select>');

// apply styles on select menu
$("#sm").selectmenu();

// add options to controlgroup 
$( "#test" ).controlgroup( "option", "corners", true );

// create controlgroup
$( "#test" ).controlgroup().trigger('create');

controlgroup div ID is #test.

Omar
  • 32,302
  • 9
  • 69
  • 112
1

I think the main problem is that every time the "Push me!!" button is pressed, you are appending a select element with an id attribute of sm. Having multiple elements with the same id is invalid HTML and can cause problems with Javascript. See this question.

Namely, the $("#sm") line doesn't know which select you are trying to target.

Maybe you should try something like this:

$("button").click(function () {
    $("#div_for_harakteristikinomenklatury_list").append('<select><option>Opt 1</option><option>Opt 2</option></select>');
    $("#div_for_harakteristikinomenklatury_list select:last").selectmenu();
});

Also, you should get rid of the onclick attribute for the button. You don't need it. Passing the click handler to the click function should make the function run when the button is clicked.

Community
  • 1
  • 1
Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
1

Instead of using id you should use class and apply selctmenu only on last appended select element. Check this fiddle

$("button").click(function show_filters() {
//alert("Hello");
    $("#div_for_harakteristikinomenklatury_list").append('<select class="sm"><option>Opt 1</option><option>Opt 2</option></select>');
    $(".sm:last").selectmenu();
})

Updated Fiddle

Gaurav
  • 8,367
  • 14
  • 55
  • 90
0

change the $("#sm").selectmenu(); to $("select").selectmenu();

With above mentioned method no matter what is the id or class of the element you newly pushed, it'll get the styles applied.

Check the live fiddle here http://jsfiddle.net/mayooresan/VMj4U/6/

Jay Mayu
  • 17,023
  • 32
  • 114
  • 148