0

I am creating radio buttons in run time using a loop and it will select one value of the radio buttons according to the loop in ascending order like:

radio button example image

bana is on first its selected value is "1"
apple is on second its selected value is "2"
orange is on third its selected value is "3"
lemon is on fourth its selected value is "4"

Here is the code of creating radio button count=4 and variable $var get value from outer loop this loog runs for the second time $var value will be 2 and so on so that it select the radio option in the pattren display in the pic above

for($i=1;$i<=$count;$i++)
{
$secname=str_replace(" ",'',$data[0]);
if($i==$var)
{
echo("<input type='radio' name='$secname' value='$i' checked='checked'>$i");
}
else
{
echo("<input type='radio' name='$secname' value='$i'>$i");
}
//  echo("<input type='hidden' name='rad[]' value='$i'/>");
}

How to swap the radio button value on change such that when i select apple radio value "1" it will change the bana value to "2" interchange its value

Nanne
  • 64,065
  • 16
  • 119
  • 163
Syed Raza
  • 331
  • 2
  • 13
  • 35

2 Answers2

0

Assuming that 'apple' and 'bana' stand for the radio button's name attribute, you could accomplish what you want using jQuery:

$(document).ready(function(){
   $('input[name="apple"]').change(function(){
      $('input[name="bana"]').each( function(){
         $(this).val( $(this).val() == 1?2:1); // if value is 1, change it to 2, otherwise to 1.
      });
   });
});
Taher
  • 11,902
  • 2
  • 28
  • 44
  • yup you right they are radio button name but radio button name and values may vary if there are 6 fruits then there would be six radio buttion and i would change any radio button value if there would any value previously exsis then it would interchang it – Syed Raza Sep 20 '12 at 07:56
0
(function () {
    var previous;

    $('input[name="'+ inputGroupName +'"]').focus(function () {
        // Store the current value on focus, before it changes
        previous = this.value;
    }).change(function() {
       // Do something with the previous value after the change
       var currentInput = this;
       var newVal = $(this).value();
       $('input').each(function(i, input) {
          if (input.value === newVal && input !== currentInput) {
            input.value = previous;
          }
    });
}());

Something like that.

Kokozaurus
  • 639
  • 7
  • 22
  • edits were made by me using http://stackoverflow.com/questions/4076770/getting-value-of-select-dropdown-before-change – Kokozaurus Sep 25 '12 at 22:08