This is probably a stupidly simple question, but I'm stuck.
I created a form with several flags, and when these flags were clicked, it passed the values of each flag in the form of a comma-separated list to a hidden input field. It worked. but due to some problems, I now have to change my code to use radio buttons instead - so the list of values separated by commas is just one at a time.
I changed the code to change it to switches, and it works fine, except for two things: 1) it does not switch between the switches (every time I select a button, it remains selected when I select the others) and 2) adding to the list instead of returning the selected single option. In other words, it still acts as a flag, except visually, it is round and has a dot. Lol
This is my code:
jQuery(document).ready(function($) { $(':radio').click(function() { var radio = $(this); var text = $('input[name="cat"]'); if(radio.is(':checked')) { text.val(text.val() + radio.val());
therefore, when I uncomment this line of "alert", I can see what I passed, and just adds to the list instead of replacing it with a new value. Does anyone know how to fix this, so you can choose and transfer only one option? (The above code works fine, like a checkbox. I really think I got the code above somewhere, although I can't remember where!)
EDIT: here is the code that solved the problem and works, (thank you all :)) - maybe this will help someone else in the future.
`jQuery(document).ready(function($) { $(':radio').change(function() { var radio = $(this); var text = $('input[name="cat"]'); if(radio.is(':checked')) { text.val(radio.val()); `
jquery forms
Shelly
source share