user1815176 user1815176 - 6 months ago 9
Javascript Question

jQuery stacking radio buttons

I am trying to make a link that changes addresses based on input from radio buttons, but the code that I have now just disregards the first radio button.

var mwc1 = $("#mwc1").is(":checked");
var mwc2 = $("#mwc2").is(":checked");
var mwv2 = $("#mwv2").is(":checked");
var mbc2 = $("#mbc2").is(":checked");
var addr = "";
if (mwc1) {
if (mwc2) {addr = "1";}
else if (mwv2) {addr = "2";}
else if (mbc2) {addr = "3";}
}
$("#checkout a").attr('href', addr);


Here is a jsfiddle of the whole code. Thanks!https://jsfiddle.net/a2Lkgua3/2/

DBS DBS
Answer

In your JS fiddle you're reusing the variable names

var mwc1 = $("#mwc1").is(":checked");
var mwv1 = $("#mwv1").is(":checked");
var mbc1 = $("#mbc1").is(":checked");
var mwc1 = $("#mwc2").is(":checked");
var mwv1 = $("#mwv2").is(":checked");
var mbc1 = $("#mbc2").is(":checked");

I assume this is a typo and you actually wanted:

var mwc1 = $("#mwc1").is(":checked");
var mwv1 = $("#mwv1").is(":checked");
var mbc1 = $("#mbc1").is(":checked");
var mwc2 = $("#mwc2").is(":checked");
var mwv2 = $("#mwv2").is(":checked");
var mbc2 = $("#mbc2").is(":checked");
Comments