Apply attribute to nested tag that is stored in a variable in a jQuery wrapper

时间:2015-06-25 18:29:54

标签: javascript jquery

I have a JavaScript variable that is in a jQuery wrapper.

var script1 = $("<label><input type='checkbox' value='script_"
                + scripts[i].id + "' id='pre_script_" + scripts[i].id
                + "'> " + scripts[i].name + "</label>);

I want to apply an attribute to the input tag. If I try simply

script1.attr('checked', true);

then the <label> tag gets the attribute. I also tried

$(script1 + " input").attr('checked', true);

but this doesn't work. Is there a way to apply this attribute to the input tag instead?

1 个答案:

答案 0 :(得分:2)

script1.find("input").attr('checked', true);