1

I am providing multiple check-box and radio button when i enter any value in text field than the same value will assign to radio button and How it is possible using jQuery. Below is the image for reference. I am storing Question and multiple answer and i am going to store true answer out of them like shown in image.

Example shown in image

$data = array();
for($i=0,$j=1;$i<count($_REQUEST['quiz_options']);$i++,$j++)
{
    $data["quiz_Options".$j] = $_REQUEST['quiz_options'][$i];
}
$data["quiz_Id"] = $Quiz_ID;
$data["quiz_Correct_Answer"] = $_REQUEST['quiz_opt'];
$quiz->insertOptions($data,'quizoptions');

<input  class="col-md-4"  type="text" name="quiz_options[]" value=""/>
<input  type="radio" name="quiz_opt[]" value=""/>

//Here I want to assign my textfield value to the radio button when i write something in my text field.

Please see the example here

Bhatt Akshay
  • 159
  • 1
  • 14

3 Answers3

2

You could attach a keydown event to the the textbox and update the value of the radiobutton.

something like this(pseudocode):

$('#textboxId').on('keydown',function(){
  $('#radiobuttonId').val($(this).value);
});
Arno Chauveau
  • 990
  • 9
  • 14
  • Yes Arno I got it like this way but still some missing... My first field is static on the page it gives me proper output, but when i click on add more button than it does not work the remaining field is generated with the help of jQuery – Bhatt Akshay Feb 26 '16 at 12:26
  • @Bhatt Akshay could you post your code in a jsfiddle so i can help you better? – Arno Chauveau Feb 26 '16 at 12:37
  • i fixed it here: https://jsfiddle.net/0vgvg6g6/1/ Your problem was that your keyup event didn't fire on dynamically added elements look here for more info: http://stackoverflow.com/questions/1359018/in-jquery-how-to-attach-events-to-dynamic-html-elements – Arno Chauveau Feb 26 '16 at 13:12
  • What is data-rdb inside attr('data-rdb') – Bhatt Akshay Feb 26 '16 at 13:19
  • I added a data-Attribute(https://developer.mozilla.org/en/docs/Web/Guide/HTML/Using_data_attributes) in your textfields that links to the radiobuttons. So we have a reference to the radiobutton – Arno Chauveau Feb 26 '16 at 13:24
  • Its not working when i generate my last three text field using my external js file. – Bhatt Akshay Feb 29 '16 at 05:13
  • could you please check your console for error messages or make a jsfiddle where you recreate the problem? – Arno Chauveau Feb 29 '16 at 07:47
0

Maybe you can use a data attributes (HTML5) Example :)

0

You have to on blur or onchange and should use one class for all dynamic textbox creation. try this code

<input name="quiz_options[]" class ="changesNo1" value="" />
<input name="quiz_options[]" class ="changesNo1" value="" />
<input name="quiz_options[]" class ="changesNo1" value="" />
<input type=radio value="" name="quiz_opt[]">
<input type=radio value="" name="quiz_opt[]">
<input type=radio value="" name="quiz_opt[]">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var test_arr = $("input[name='quiz_options[]']");
$(document).on('change keyup blur','.changesNo1',function(){
 $.each(test_arr, function(i, item) {
  $("input[name='quiz_opt[]']").val($(item).val());
  var d=$("input[name='quiz_opt[]']").val(); // your reference
  //alert(d); // This alert for your reference
 });
});
</script>
Gopalakrishnan
  • 957
  • 8
  • 19