0

I have a form with two sets of radio buttons. I am trying to make it so that when a certain value is checked, a <p> element (with the Id descript) will update with the corresponding data.

Here is what I have, it isn't updating the element at all right now.

DEMO

function classStats() {
  classes = ['archer', 'mage', 'warrior'];
  classStats = ['HP: 20 Strength: 3 Intellect: 1 Speed: 5 Magic Defense: 1 Defense: 3', 'HP: 15 Strength: 1 Intellect: 6 Speed: 2 Magic Defense: 2 Defense: 1', 'HP: 30 Strength: 2 Intellect: 1 Speed: 1 Magic Defense: 3 Defense: 5'];
  classAdd = ['The archer also has a special passive for armor penetration.', 'The mage has a special passive for increased gold gain', 'The warrior has a special passive for percent damage mitigation.'];
  for (i = 0; i < 3; i++) {
    c = classes[i];
    e = classStats[i];
    f = classAdd[i];
    if ($('input[name=class]:checked').val() === c) {
      $('#descript').text(e + ' ' + f);
    }
  }
}
classStats();
Hawken MacKay Rives
  • 1,171
  • 1
  • 16
  • 26
Shniper
  • 854
  • 1
  • 9
  • 31

2 Answers2

1

There are multiple problem in your code :-

1.You are not listening radiobutton change event.

2.There is no need of looping.

Below is the modified and optimized version of your code.

var classes = ['archer', 'mage', 'warrior'];
var classStats = ['HP: 20 Strength: 3 Intellect: 1 Speed: 5 Magic Defense: 1 Defense: 3', 'HP: 15 Strength: 1 Intellect: 6 Speed: 2 Magic Defense: 2 Defense: 1', 'HP: 30 Strength: 2 Intellect: 1 Speed: 1 Magic Defense: 3 Defense: 5'];
var classAdd = ['The archer also has a special passive for armor penetration.', 'The mage has a special passive for increased gold gain', 'The warrior has a special passive for percent damage mitigation.'];
var c,e,f;

$('input[name=class]').change(function(){
    c = classes.indexOf($(this).val());
    e = classStats[c];
    f = classAdd[c];
    $('#descript').text(e + ' ' + f);
});

DEMO

Community
  • 1
  • 1
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
-1

Add the onchange events for your radio buttons on document ready. And call your code when the event is fired.

$('input[name=class]').change(function () {
     classes = ['archer', 'mage', 'warrior'];
     classStats = ['HP: 20 Strength: 3 Intellect: 1 Speed: 5 Magic Defense: 1 Defense: 3', 'HP: 15 Strength: 1 Intellect: 6 Speed: 2 Magic Defense: 2 Defense: 1', 'HP: 30 Strength: 2 Intellect: 1 Speed: 1 Magic Defense: 3 Defense: 5'];
     classAdd = ['The archer also has a special passive for armor penetration.', 'The mage has a special passive for increased gold gain', 'The warrior has a special passive for percent damage mitigation.'];
     for (i = 0; i < 3; i++) {
        c = classes[i];
        e = classStats[i];
        f = classAdd[i];
        if ($('input[name=class]:checked').val() === c) {
          $('#descript').text(e + ' ' + f);
        }
     }
});

Look at How to use radio on change event? for details on how to add events for radio buttons.

Community
  • 1
  • 1
Teja
  • 1,236
  • 12
  • 27