Mohamed Hussien Mohamed Hussien - 5 months ago 7
jQuery Question

Generating text by selecting Radio Button

I've created something like a survey, I have a bunch of questions with yes or no answers, (every couple has the same name property).

I need to insert text string into textarea if the user chooses "Yes", and replace that text with something else if he clicked "No"... etc till the end of the questions.

HTML

<div style="display: none;" class="s_node" id="child_of_option_1_node_16">
<p>
<input id="option_2" name="plan" value="Choice 1 text string..." type="radio">
<label id="option_2" for="option_2">Solved</label>
</p>
<p>
<input id="option_3" name="plan" value="Choice 2 text string..." type="radio">
<label id="option_3" for="option_3">Not-Solved</label>
</p>
</div>


JS

$(document).ready(function() {
if ($('#option_1').is(':checked')) {
$('#child_of_option_1_node_16').show('fast');
} else if (!$('#option_1').is(':checked')) {
$('#child_of_option_1_node_16').hide('fast');
clearradios(child_of_option_1_node_16);
}
$('#option_1').parent().bind('click', function() {
if ($('#option_1').is(':checked')) {
$('#child_of_option_1_node_16').show('fast');

document.GenerationForm.myTextArea.value += document.getElementById("CstName").value + ",,,";

} else if (!$('#option_1').is(':checked')) {
clearradios(child_of_option_1_node_16);
hidechildren(child_of_option_1_node_16);
$('#child_of_option_1_node_16').hide('fast');

}
});
});

Answer

This might work

<div style="display: none;" class="s_node" id="child_of_option_1_node_16">
  <p>
    <input id="option_2" onclick="changeText(this)" name="plan" value="Yes" type="radio">
    <label id="option_2" for="option_2">Solved</label>
  </p>
  <p>
    <input id="option_3"  onclick="changeText(this)" name="plan" value="No" type="radio">
    <label id="option_3" for="option_3">Not-Solved</label>
  </p>

<script>
function changeText(e){
   var value = e.value;
   var textbox = document.getElementById("yourTextBoxId");

   textbox.value = value;
}
</scrip>

Just a thought...