0

Quick question I'm unable to resolve... I have a radio button with values of yes or no.

Basically I'm doing this:

if (complete === '1') {
getElementById('test-radio').value('Yes')
}

But it's not setting this to yes, it's still defaulting as no...

Any advice on how I can make this work?

  • 1
    `value` is not a function. Also, setting the `value` of a radio button will replace the value contained on the given node. What you want to do is changed the `checked` state of the radio button. – zzzzBov Jul 19 '17 at 17:00
  • sorry value() is the API's version of val(). –  Jul 19 '17 at 17:44
  • what API are you referring to? You've tagged [tag:javascript] which implies you're using the native DOM APIs which `getElementById` appears to be (of course that's missing `document` as well), so I had assumed you were attempting to use the [`value` property on `HTMLInputElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) which is *not* a function. If there is another API that you are using you will need to tag your question with that as well as it significantly impacts the answer. – zzzzBov Jul 19 '17 at 17:58

1 Answers1

0

You can use either radioButton.checked=true;

What you are doing isn't javascript syntax.

if (complete === '1') {
  getElementById('test-radio').checked=true;
 }
Joey Pinto
  • 1,735
  • 1
  • 18
  • 34
  • Check my edit, accept the solution if you are satisfied – Joey Pinto Jul 19 '17 at 17:03
  • This didn't work either, do you think it's because the values of the radio are yes or no, it's not true/false. –  Jul 19 '17 at 17:45
  • does javascript reach the if body, place an alert inside the if block and check. Also look for any errors on the console. – Joey Pinto Jul 19 '17 at 17:54
  • @kyle "do you think it's because the values of the radio are yes or no, it's not true/false" the value of a radio button--which is always a string--is completely irrelevant to the checked state of the radio button--which is always a boolean. – zzzzBov Jul 19 '17 at 18:01