0

I have different groups of radiobuttons and all of the radiobuttons witch is paired have the same id so that they should work togheter and they do in certain ways. But when I want to validate I use this method:

if ($("PhysicallyActive").is(":not(':checked')")) {
    $("PhysicallyActiveradio").css("border", "2px solid red");
}
else {
    $("PhysicallyActiveradio").css("border", "");
}

With this method it only checks if the first radiobutton of the two or three radiobuttons is checked. So if the secound radiobutton is checked it I want it to go to else and reset the border. But as it is now I will set a red border if the second radiobutton is checked. I want it to check if any of the radiobuttons is checked that have the same id. How can I do that?

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Daniel Gustafsson
  • 1,725
  • 7
  • 37
  • 78
  • [jQuery validation radio buttons](http://stackoverflow.com/questions/15773244/jquery-validation-radio-buttons) – TravJenkins Jul 26 '13 at 12:02
  • Please include the relevant html code and the complete javascript function – havardhu Jul 26 '13 at 12:04
  • 1
    "have the same id so that they sould work together". No, this doesn't make sense because the ID needs to be unique. What they should share is the name. – emerson.marini Jul 26 '13 at 12:04
  • Your jQuery code is wrong plus in your question, you mentioned same `id` thing, which is not good practice. consider [jQuery API documentation for selectors](http://api.jquery.com/category/selectors/) – Dhaval Marthak Jul 26 '13 at 12:07

3 Answers3

1

"have the same id so that they should work together"

That's the problem. When you set the same ID to multiple elements, just the first one is selected, no matter what.

This doesn't make sense because the ID needs to be unique.

What they should share is the name.

As @Alxandr pointed out, you can then select the elements by their name like this:

$("input[name='myname'][type='radio']")
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
emerson.marini
  • 9,331
  • 2
  • 29
  • 46
  • how do i access the elements by name in jquery? by id i do: $("#elementid") – Daniel Gustafsson Jul 26 '13 at 12:21
  • 1
    @DanielGustafsson `$("input[name='myname'][type='radio']")` for instance. – Alxandr Jul 26 '13 at 12:25
  • @Alxandr just answered. – emerson.marini Jul 26 '13 at 12:26
  • _"When you set the same ID to multiple elements, just the first one is selected, no matter what.."_ - That's not strictly true. `getElementById()` only ever returns one (usually the first, but not in all browsers), but with `querySelectorAll()` or jQuery if your selector has tagname + ID (`"input#idhere"`) it will select all of them. But it is still a bad idea to do that in your markup, ID _is_ supposed to be unique. – nnnnnn Jul 26 '13 at 12:30
  • @nnnnnn that's probably due to the fact that jQuery probably optimizes simple id-selectors to use `getElementById`. – Alxandr Jul 26 '13 at 12:32
  • now i have this if ($("input[name='Gender'][type='radio']").is(":not(':checked')")) { $("#Genderradio").css("border", '2px solid red'); } and if the second radiobutton is checked it will return true :S – Daniel Gustafsson Jul 26 '13 at 13:08
1

Set the Id's for them to be different but assign them the same class

class="PhsicallyActive";
class="PhysicallyActiveradio"


if ($('.PhysicallyActive').is(":not(':checked')")) {
$('.PhysicallyActiveradio').css("border", "2px solid red");
}
else {
    $('.PhysicallyActiveradio').css("border", "");
}
Malcor
  • 2,667
  • 21
  • 29
0

I miss understood, are you after something like this?

$( "radio" ).each(function( index ) {

  if index + ": "not(':checked')")) {
$('.PhysicallyActiveradio').css("border", "2px solid red");
}
else {
    $('.PhysicallyActiveradio').css("border", "");
}
    });
Malcor
  • 2,667
  • 21
  • 29