2

I have 3 radio buttons and a parragraph. What I have to do is to check a button, and then, change the color of the paragraph.

<p id="pColor">Este párrafo cambiará de color según el valor del radio seleccionado.</p>

<form name="formColor">
    <input type="radio" name="color" value="red" id="red"/>Rojo
    <input type="radio" name="color" value="green" id="green"/>Verde
    <input type="radio" name="color" value="blue" id="blue"/>Azul
</form>

This is my jQuery code:

$(document).ready(function(){
    $("input[name='color']").change(function(){
        cambiaColor()
    });
});

    var cambiaColor=function(){
        $("input").each(function(){
            $("#pColor").css("color",$(this).val());
        });

    }

The text always turn blue, no matter what button I checked

Wanna Coffee
  • 2,742
  • 7
  • 40
  • 66
Alex
  • 750
  • 5
  • 15
  • 33
  • possible duplicate of [How can I get which radio is selected via jQuery?](http://stackoverflow.com/questions/596351/how-can-i-get-which-radio-is-selected-via-jquery) – Cerbrus Jan 31 '14 at 08:00

6 Answers6

2

Why you are using .each for that? Use onClick instead

$("input").on('click', function(){
    $("#pColor").css("color",$(this).val());
});

Demo


All you need is the above code, nothing else... Demo, can also replace $(this).val() to this.value as pointed out by @Cerbrus


Note: I've just used input as a selector there which is too general, so make sure you make it specific by using $('input[name="color"][type="radio"]')


Also, make use of label, will enhance user experience, it's not comfortable to press the radios always...

Demo (Click the text and the radios will be selected)

Community
  • 1
  • 1
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
1

it should be

$(document).ready(function () {
    $("input[name='color']").change(function () {
        $("#pColor").css("color", this.value);
    });
});

Demo: Fiddle

in your case you are iterating over all the input elements and calls the .css(...) in the loop which sets the value in the loop - so only the last value will be applied

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

because you are iterating through all radio button, so it will change its color one by one and finally it will apply the last one.

you should try

$(document).ready(function () {
    $("input").on('click', function(){
        $("#pColor").css("color",$(this).val());
    });
});
Raj
  • 307
  • 1
  • 5
0

Of course, because you are looping all options and everytime you assign color - it will always end with last one. You have to modifiy your JS code like this:

$(document).ready(function () {
   $("input[name='color']").change(function () {
      $("#pColor").css("color", $("input[name='color']:checked").val());
   });
});

Check this JSFiddle.

Pavel Štěrba
  • 2,822
  • 2
  • 28
  • 50
0

Because you are using the loop and it internally changes for all the three but after everything seems to be over you see only the last change.

Come on it is in a loop.

var cambiaColor=function(){
    $("input").each(function(){
        $("#pColor").css("color",$(this).val());
    });

}

Analysis,

each loop,

1st iteration:
pColor will have the color RED

2nd iteration:
pColor will have the color GREEN

3rd iteration:
pColor will have the color BLUE

Since computational tasks happen in less than nano second your eye is unable to adjust itself for all the 3 changes and finally settling only for the final change.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
0

That is because your loop will simply iterate over all the input fields, not only the selected one. This will result in the last input element's value being used, which in your case is the one with as its value "blue". That is why you always end up with blue.

You should be using the checked input items's value. You can do this as follows:

var cambiaColor = function() {
     $("#pColor").css("color", $('input[name='color']:checked').val());
}
Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81