0

This is my first real attempt to write a JS function. I want to set the value of a text field depending on the selction of a pair of radio buttons.

<script type='text/javascript'>

function setInputValue(){

if(document.getElementById('choice_8_0').checked) {

       document.getElementById('input_6_18').value = '1';

    }else if(document.getElementById('choice_8_1').checked) {

     document.getElementById('input_6_18').value = '0';
}}

</script>

Can anyone help me with where I went wrong? I am not even sure how to name the function really.

2
  • Can you post a sample of your html as well which is invoking this function? Nov 21, 2012 at 17:17
  • You can see form on this page hookahi.com/guides/hucks-charters/
    – Kyle
    Nov 21, 2012 at 19:35

2 Answers 2

2

You could use:

 document.getElementById('elementId').checked = true;

or

document.getElementById('elementId').checked = false;
0

To select a radiobutton, you can use the checked property of the radiobutton:

 document.getElementById('input_6_18').checked = true/false;

EDIT: seems like you want to set the text of an input type text?

Than your code is correct, the property is value. So what is the problem?

7
  • I think the if condition is already ensuring that whether the checkbox(which is with id '#choice_8_x' ) checked or not Nov 21, 2012 at 17:17
  • Yes, i thought he wanted to set the value of another radio button.. but i don't know what his question really is, to be honest.. Nov 21, 2012 at 17:22
  • My fault for now clarifying. The input_6_18 box is a standard text field. Depending on which radio button is selected I would like to automatically fill the text field with either 0 or 1
    – Kyle
    Nov 21, 2012 at 19:27
  • At this point the problem is simply that nothing is happening when I select the radio buttons. That's why I sought help
    – Kyle
    Nov 21, 2012 at 19:31
  • Please post all of your code, also where do you call this function? Nov 22, 2012 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.