2

I have a radio input:

<INPUT TYPE="radio" NAME="test" id="test" VALUE="product" CHECKED>Product<BR>
<INPUT TYPE="radio" NAME="test" id="test" VALUE="company">Supplier<BR> 

now I want to set a radio's value to a hidden value. like this:

<input type="hidden" name="module" id="hdModule" onchange="document.getElementById('test').value=this.value" />

but it doesn't work.

7
  • Hidden input values cannot be changed, except by javascript. The on-change call will never fire unless there's some javascript I'm not seeing. Commented Dec 5, 2014 at 22:41
  • 1
    you'll have to scan for the one with the right value and check it I think, you can't have Id on two elements the same. But why are you doing this, it seems weird to put an onchange on an hidden element because since it is hidden, the user generally won't be able to change it. Commented Dec 5, 2014 at 22:41
  • Do you mean you want to set which radio button is checked based on the hidden input, or that you want the hidden input's value to change when a new radio button is selected? I'm not quite understanding the context. Commented Dec 5, 2014 at 22:43
  • i want to build a search site. when i checked radio 1: it'll search by value 1. if i checked radio 2: it search by value 2. i want the hidden inputs value have value of radio when i check. how i do it? Commented Dec 5, 2014 at 22:48
  • 1
    I think the easiest way to do that is to just use the value of test that is sent to the server. You shouldn't need a hidden element at all. When a radio button is sent to the server side app, the value of the name field is only the one currently selected. Commented Dec 5, 2014 at 22:49

2 Answers 2

2

Please try to put the following line inline the tow radio buttons

onclick="document.getElementById('hdModule').value=this.value"  

You need to remove the onchange from the hidden input
and add
value="product"

Sign up to request clarification or add additional context in comments.

2 Comments

i do as you ask. it's work too. but the first time. i don't checked supplier. it's don't work. i must checked supplier. and checked product again it's work? how can i fixed it?
Ok just add value="product" to hidden input
1

Well, you are doing it the opposite of what you describe in your comments. If you want the hidden variable to hold the value of what you selected you should do this:

<INPUT TYPE="radio" NAME="test" id="test" VALUE="product" onchange="document.getElementById('hdModule').value=this.value" chECKED>Product<BR>

<INPUT TYPE="radio" NAME="test" id="test" VALUE="company" onchange="document.getElementById('hdModule').value=this.value">Supplier<BR> 

<input type="hidden" name="module" id="hdModule"  />

You can verify if it works by changing the type on the last element to text and see in a browser (I tried in chrome)

1 Comment

i do as you ask. it's work. but the first time. i don't checked supplier. it's don't work. i must checked supplier. and checked product again it's work? how can i fixed it?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.