socramm socramm - 4 months ago 11
HTML Question

html add class to item without ID with js

I want to add a class to a

<li>
using JavaScript, this is the html I have:

<li tabindex="-1" role="option" unselectable="on" class="k-item">
<input type="checkbox" name="categoryItems_option_1" value="1">&nbsp;
<label for="categoryItems_option_1" class="selctAllBox">Select/Deselect All</label>
</li>


I cant use the "k-item" class because there is more then one of them.

I added the "selectAllBox" class like that:

$("label[for='categoryItems_option_1']").addClass("selctAllBox");


Is there a way to use something like parent or anything like that, to set the class?

Answer

You can use the parent, like so:

$("label[for='categoryItems_option_1']").parent().addClass("TheClass");
Comments