How to create an element with levels and then add elements to one of its sub-levels with jquery?

4

I need to create the object with the structure below and insert options.

 myElem = $("<div class='row'> <div class='form-group'> <label for='multiple-selected' class='col-md-offset-1 col-md-2 control-label'>Teste</label> <div class='col-md-5'><select id='multiple-selected' multiple='multiple'></div> </div> </div>");

And then insert option on that object. I do not want to do concatenating pure text (by putting the html in hand). Can you do this with append? I thought of appendTo () but the object has not yet been inserted into the html would anyone have a hint?

    
asked by anonymous 19.10.2015 / 23:13

1 answer

3

Even without the element being inserted into the DOM you can use jQuery methods.

Test with var $select = myElem.find('select');

var myElem = $("<div class='row'> <div class='form-group'> <label for='multiple-selected' class='col-md-offset-1 col-md-2 control-label'>Teste</label> <div class='col-md-5'><select id='multiple-selected' multiple='multiple'></div> </div> </div>");
var $select = myElem.find('select');
console.log($select); // dá [select#multiple-selected, prevObject: jQuery.fn.init[1], context: undefined, selector: "select"]
    
19.10.2015 / 23:15