I'm trying to pass the value of a checkbox only if it is checked, I'm using a text input. I'm running into a syntax issue.. I don't see where I missed any brackets or anything. Any help is appreciated. Fiddle is here also
Thanks
document.querySelector(function() {
setTarget();
document.querySelector("#test").change(setTarget);
function setTarget() {
var tmp ="";
tmp = document.querySelector("#test:checked").value || '';
document.querySelector('#testtarget').val(tmp);
}
});
<input type="checkbox" id="test" name="test123" value="testing123" />this should appear in the text input<br/>
<input type="text" id="testtarget" name="targetTextField" size="31" tabindex="0" maxlength="99" value="">
CodePudding user response:
to select checked checkboxes only you can use :checked
with your querySelector
use it this way
document.querySelectorAll("input[type=checkbox]:checked")
if i am not wrong this is what you exactly want
var cb = document.querySelector("#test"),
inp = document.querySelector("#testtarget")
cb.onchange = function(e){
inp.value = cb.checked ? cb.value : ""
}
<input type="checkbox" id="test" name="test123" value="testing123" />this should appear in the text input<br/>
<input type="text" id="testtarget" name="targetTextField" size="31" tabindex="0" maxlength="99" value="">