sarah.ferguson sarah.ferguson - 1 year ago 69
jQuery Question

jquery .attr() after .append()

I'm generating a select option programatically however the

function after an
doesn't add the attribute to the just appended element but to the father.

return the just appended element?

Here a JSFiddle example

$("body").html($("<select>").append("<option>").attr('value',"bla bla bla").append("</option>").append("</select>"));

instead of getting a

<select><option value="bla bla bla"></option></select>

I get a

<select value="bla bla bla"><option></option></select>

Answer Source

Try this out:-


$("body").append($("<select>").append($("<option>").attr('value',"bla bla bla")));