alex johnson alex johnson - 8 days ago 5
jQuery Question

Modity HTML within JQuery for checkboxes

I currently have the following function in Jquery which returns the correct set of information:

function checkAllPriceBuilderBoxes() {
var contentzz = $("#props #itemEditor #builderItemsList .listItem .controlCtr").html();
console.log(contentzz)
}


contentzz
returns the following html:

<input class="control" style="" type="checkbox" _type="checkbox" value="No">


I am looking to modify this html adding the 'checked' text at the end of the html:

<input class="control" style="" type="checkbox" _type="checkbox" value="No" checked>


I have tried the following but does not work.

$(contentzz).append("checked");


ideas?

Answer

You need to add attribute to element. use .attr():

$(contentzz).attr("checked",true);

If you want the modified string after adding attribute, then you need to to overwrite contenzz string with modified string element.

contentzz = $(contentzz).attr('checked',true)[0].outerHTML;
Comments