Change boolean field value with JS [closed]
-
01-01-2021 - |
Frage
I have to add some field value on a form using JS/JQ. I could manage text, number and choice fields but I am stuck on Boolean ones:
$("input[title='Title Required Field']").val("Hey my dear");
$("textarea[title='Task Description']").val("Mary had a little lamb");
$("select[name='choicefieldname'").val("Choice#3");
Work fine, while
$("input[name='YesNotFieldName']").val(true);
does not work. Boolean fields have no "title" tag, So I have to use "name". I have also tried with "id" but it did not help.
Also I have tried giving value "1" but still does not work.
Where is the mistake? Thanks
Lösung
Try using .prop
to deal with boolean
that is for supported attributes like selected/disabled/checked
e.t.c
$('#element').prop('attribute', true);
from jQuery docs, (an example)
elem.checked
returns true
(Boolean) Will change with checkbox state
$(elem).prop("checked")
returns true
(Boolean) Will change with checkbox state
elem.getAttribute("checked")
returns "checked"
(String) Initial state of the checkbox; does not change
$(elem).attr("checked")(1.6)
returns "checked"
(String) Initial state of the checkbox; does not change
$(elem).attr("checked")(1.6.1+)
returns "checked"
(String) Will change with checkbox state
$(elem).attr("checked")(pre-1.6)
returns true
(Boolean) Changed with checkbox state