- This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
Viewing 5 posts - 1 through 5 (of 5 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › How do I get the values of radio buttons?
Someone please help.
I created a form where registrants have to select a radio button and a hidden div will be displayed based on their selection. The issue I have now is that I can not get values from the radio buttons. When I put any value in the input tag/element, the jQuery that enables the display of the hidden div stops working. What do I do to make it all work.
The code is right below:
and the jQuery is below.
$("#step_2 input[name=religion_group]").click(function(){
if ($("#christianity:checked").val() == 'on') {
$("#christian").slideDown();
} else {
$("#christian").slideUp();
};
if ($("#others:checked").val() == 'on') {
$("#other_religion").slideDown();
} else {
$("#other_religion").slideUp();
};
});
You have no need for the ‘:checked’ part of that if statement, as you are already checking to see if the value == ‘on’. One or the other should be sufficient.
Thanks for your response but I don’t think you understand my question. I want to get the value christian when a user selects Christianity, muslim when he selects Muslim or others when he selects Others and still retain the div hide/show. Presently what I get in my database is the value on when either christianity or others is selected and null when muslim is selected.
Oh right, sorry, did not understand the question!
You need to give the Radio buttons a value attribute for them to pass a value to the database
So:
Thank you, I got it fixed. When I gave it a value before, the jQuery stopped working but I realized I had to change the val() in jQuery to the value I gave to it in the form. I have been using val() == ‘on’ before now.
Thanks a lot for your help!