-2

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);

I dont need to se if its checked I need to change it to checked

Mntfr
  • 483
  • 6
  • 20

2 Answers2

2

<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>
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

<input> is an empty element. You can use <label> element, for attribute.

To check <label> element .htmlFor elements use .control.checked ="checked"`

<input type="checkbox" id="permission" name="permission[]" value="2">
<label for="permission">Access</label>
<script>
document.querySelector("[for=permission]").control.checked = "checked";
</script>
guest271314
  • 1
  • 15
  • 104
  • 177
  • How does he use this in the Javascript to check the box based on the label containing `Access`? – Barmar Nov 09 '16 at 23:22
  • @Barmar Click the ` – guest271314 Nov 09 '16 at 23:23
  • He wants to use Javascript to check it. – Barmar Nov 09 '16 at 23:23
  • @guest271314 it is still not related with the question. He wanted to check based on the text when document is ready. – Ahmet Can Güven Nov 09 '16 at 23:30
  • he wants to find the checkbox based on the text "Access" in the label. If you're going to hard-code the ID of the checkbox, what's the point of going through the label? – Barmar Nov 09 '16 at 23:30
  • You might as well just write `document.querySelector("#permission").checked = "checked"` – Barmar Nov 09 '16 at 23:30
  • @Barmar The actual Question is _"In jquery how can I check a checkbox by its text"_ . Given that `` elements do not have text, the ` – guest271314 Nov 09 '16 at 23:33
  • The input to whatever he's doing is the string "Access". He wants to find the checkbox whose label contains that string, without knowing its ID. – Barmar Nov 09 '16 at 23:34
  • @Barmar Did not interpret Question the same way, here. Though that is ok. ` – guest271314 Nov 09 '16 at 23:37
  • What do you think he means by "based on the input´s text"? He wants to find the string "Access", then check the checkbox next to it. If he knew the ID of the checkbox, he wouldn't need to search for it based on the text. – Barmar Nov 09 '16 at 23:38
  • @Barmar _"What do you think he means by "based on the input´s text"?"_ Given `` element is empty element, where the input does not contain "input's text", that ` – guest271314 Nov 09 '16 at 23:41
  • Yes, the label is appropriate. But you still have to search for the label by its text, not by its `for=` attribute. – Barmar Nov 09 '16 at 23:42
  • You should find the label that contains `Access`, then use `document.getElementById(label.for).checked = 'checked';` – Barmar Nov 09 '16 at 23:43
  • Why would you search by text? Text can be anything. Use the direct linkage feature of `.control`. Ok, can do that as well. Though would probably suggest using `data-*` attribute having value `"Access"`. Not sure if the effective result would be different from using approach at present Answer? – guest271314 Nov 09 '16 at 23:44