change event on radiobutton click
Hello and thank you to everyone who has previously helped me.
This time I don't have a problem as my code is working. I am only asking if there is a more efficient way (i.e. less code) to accomplish my goal of utilizing a show hide based on a radio button clicked.
In the jsfiddle above, if you click the Other button, a required textfield appears. If you click any other radio button, the textfield disappears and is no longer required.
It's the hide part of the code, below, that I figure there is a more efficient way to write.
-
- $("#semester_1, #semester_2, #semester_3, #semester_4").change(function() {
- var checked =this.checked
- if(checked){
- $('.semester').hide();
- $('#short_field_003').removeClass("required error");
- $('#short_field_003').val("");
- }
- }).change();
I tried the following but it didn't work.
-
- $("#semester_5").change(function() {
- var checked =this.checked
- if(checked){
- $('.semester').show();
- $('#short_field_003').addClass("required error");
- }
- if(!checked){
- $('.semester').hide();
- $('#short_field_003').removeClass("required error");
- $('#short_field_003').val("");
}
Thanks again. Any help in pointing me in the right direction will be greatly appreciated!