Mntfr Mntfr - 25 days ago 10
Javascript Question

In jquery how can I check a checkbox by its text

Lets say I have

<input type="checkbox" name="permission[]" value="2">Access</input>


Is there a way to check the checkbox based on the input┬┤s text?
something like

Access.prop('checked', true);

Answer

<input> elements don't have any text, because they're not containers. You can wrap it in a DIV or some other container element, and then search for that DIV by its contents with :contains.

$(":checkbox").parent(":contains(Access)").children(":checkbox").prop('checked', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div><input type="checkbox" name="permission[]" value="2">Access</div>
<div><input type="checkbox" name="permission[]" value="2">Something else</div>

Comments