Mntfr Mntfr - 1 year ago 66
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 Source

<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=""></script>
<div><input type="checkbox" name="permission[]" value="2">Access</div>
<div><input type="checkbox" name="permission[]" value="2">Something else</div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download