use checkboxes as radio buttons

I needed to compliment my previous post – Javascript : form validation – only 1 checkbox can be checked by using checkboxes like radio buttons. This code will uncheck all the boxes except for the one that you check. Thus providing radio button functionality.

function toggleRadio(checkbox) {
		for (var i=0;i<document.getElementById('FORM_ID').elements.length;i++) {
			var e = document.getElementById('FORM_ID').elements[i];
			if (e.type=='checkbox' && e.name != checkbox.name) {
				e.checked = false;
			}
		}
	}

It is implemented on the checkbox like this:

onclick="toggleRadio(this);"

Rate this

Post a Comment
  1. Leave this field empty

Required Field