Search code examples
jqueryvariablesonclickradio-button

When Radio button is clicked a variable changes to another variable


I have a simple 2 option radio button. I would think my code would work and have tried many other versions, but I am just missing something. I need to get the var typeOfCards to change when radio button changes. The variable is used in a later equation.

input value 155 checked should change typeOfCards to equal boxCards and input value 145 checked should change typeOfCards to equal singleCards

<input name="SELECT___CD-0035___33" value="155" onclick="change_option('SELECT___CD-0035___33',this.value)" type="radio">Box Cards (Sold in Multiples of 3) <br>
<input checked="checked" name="SELECT___CD-0035___33" value="154" onclick="change_option('SELECT___CD-0035___33',this.value)" type="radio"> Single Cards (Sold in sets of 12 Cards/12 Envelopes)<br>

var singleCards = 12
var boxCards = 3
var typeOfCards = singleCards
$("input[value='155']").click(function() {
    typeOfCards = boxCards;
});
$("input[value='154']").click(function() {
    typeOfCards = singleCards;
});

Solution

  • It's possible that the handlers aren't getting attached because they aren't inside document.ready and the script is running before the elements are created.

    Try:

    $(document).ready(function(){
    var singleCards = 12
    var boxCards = 3
    var typeOfCards = singleCards
    $("input[value='155']").click(function() {
        typeOfCards = boxCards;
    });
    $("input[value='154']").click(function() {
        typeOfCards = singleCards;
    });
    });
    

    btw, if you have the jQuery click handlers, you probably don't also want an onclick.

    Check it, it works in this fiddle: http://jsfiddle.net/ccross59/4K2R7/1/