3

I've got the following error when i'm sending the form, to be exact with radio button input.

Here is the HTML Code:

      <input name="radio" type="radio" class="raido-btn" id="coral1" value="coral1" />
      <label for="coral1">CORAL I</label>
      <input name="radio" type="radio" class="raido-btn" id="coral2" value="coral2" />
      <label for="coral2">CORAL II</label>
      <input name="radio" type="radio" class="raido-btn" id="coral3" value="coral3" />
      <label for="coral3">CORAL III</label>

Here is the Javascript code:

if($('#coral1').prop('checked')){
        apartamentos = 'Coral 1';
    }
    else if($('#coral2').prop('checked')){
        apartamentos = 'Coral 2';
    }
    else if($('#coral3').prop('checked')){
        apartamentos = 'Coral 3';
    }

I don't know how to set the variable "apartamentos" to the radio button which is checked.

It always set the variable apartamentos = 'Coral'

Thanks in advance

3
  • apartamentos = $('.raido-btn:checked').val() Commented Mar 7, 2013 at 20:48
  • With that always set apartamentos = 'Coral 1' Commented Mar 7, 2013 at 21:04
  • All your radios have the same name, only one can be checked. Commented Mar 7, 2013 at 21:16

2 Answers 2

3

JQuery has a few ways to do this. One way I usually do it is by using .is(), for example, $('#coral2').is(':checked'). You could also use apartamentos = $('.raido-btn:checked').val()

Sign up to request clarification or add additional context in comments.

3 Comments

Going to try first option but with the second option always sets the variable apartamentos = 'Coral 1';
You should be able to do $("#coral2").checked and avoid little jquery overhead =)
Thank you. I add onclick="$('#coral1').checked" for every input now works perfect ;)
2

What are you attaching that if statement to? I attached it to the clickevent in jsFiddle and and it seems to be working fine. I'm not sure if you want apartamentos to be set to the value of the radio button or the strings that you defined, but it's definitely being updated.

http://jsfiddle.net/FTT5e

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.