1

Check the inputs, if value is 1 and is checked, set span color to green, if not, set to red. I know that I am not selecting the span properly either, I can't figure out how I should do that.

HTML

<input type="radio"name="rate" value="1" CHECKED>
<span class="radioLabel">Yes</span>

<input type="radio" name="rate" value="0">
<span class="radioLabel">No</span>

jQuery

$('.ratingInput input')
    .each(function() {
        if($(this).val() == ('1') && $(this).is(':selected'))
        {
            $(this + ('span')).css('border','1px solid green');
        } else {
            $(this + ('span')).css('border','1px solid red');
        }
    }
0

3 Answers 3

2

This will do what you want

$('input')
    .each(function () {
    if ($(this).val() == ('1') && $(this).is(':checked')) {
        $(this).next('span').css('border', '1px solid green');
    } else {
        $(this).next('span').css('border', '1px solid red');
    }
});
0
1

Working demo http://jsfiddle.net/UL3Cn/

few things were incorrect: :)

rest should fit the need :)

code

  $(document).ready(function () {
      $('.ratingInput input').each(function () {
          if ($(this).val() == ('1') && $(this).is(':checked')) {
              $(this).next('span').css('border', '1px solid green');
          } else {
              $(this).next('span').css('border', '1px solid red');
          }
      });
  });
2
  • 1
    thank you for such a complete answer, this is great!
    – tekstrand
    Dec 8, 2013 at 2:41
  • @TheEks Glad it helped you :) see you around!
    – Tats_innit
    Dec 8, 2013 at 2:59
1

Try

$(this).next('span').css('border','1px solid green');


fiddle Demo

$('input:checked[value="1"]').next('span').css('border', '1px solid green');
$('input[value!="1"]').not(':checked').next('span').css('border', '1px solid red');


fiddle Demo with classes

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.