$(tHTML).find('span').addClass('someClass');
As pointed out in the comments, you don't need to use $.parseHTML
, just put data
in a jQuery wrapper:
$(data).find('span').addClass('someClass');
Question
I have this string in variable name data
which have html tags and all
<div class="cqr">
<span class="ico-cqr2 icon-28 star1">quality</span>
<span class="ico-cqr2 icon-28 star2">quality</span>
<span class="ico-cqr2 icon-28 star3">quality</span>
<span class="ico-cqr2 icon-28 star4">quality</span>
<span class="ico-cqr2 icon-28 star5">quality</span>
</div>
I parse this string into html as
var tHTML = $.parseHTML(data, null, true);
now i need to manipulate the DOM like add/remove some class from span. how can I achieve that.
Solution
$(tHTML).find('span').addClass('someClass');
As pointed out in the comments, you don't need to use $.parseHTML
, just put data
in a jQuery wrapper:
$(data).find('span').addClass('someClass');
OTHER TIPS
Please look around a bit more before asking a question that:
a) Has been asked before
b) Is present on the jquery API: EDIT: All class functions
Example:
$('span').removeClass('ico-cqr2, star1');
Add class to star1
$(tHTML).find('star1').addClass('someClass');
Remove class from star1
$(tHTML).find('star1').removeClass('someClass');
You can manipulate by 'id' ou 'class' When you put the string between $ () system already handles like a jquery object
How use addClass http://api.jquery.com/addClass/ How use find http://api.jquery.com/find/