Question

this is my html code:

<form name="dform">
<fieldset>
    <input type="radio" class="yes" name="g1" value="1"/> Yes<br/>
    <input type="radio" class="no" name="g1" value="0"/> No
</fieldset>
    <fieldset>
   <input type="radio" class="yes" name="g2" value="1"/> Yes<br/>
        <input type="radio" class="no" name="g2" value="0"/> No
    </fieldset>
    <fieldset>
    <input type="radio" class="yes" name="g3" value="1"/> Yes<br/>
        <input type="radio" class="no" name="g3" value="0"/> No
    </fieldset>
    <input type="reset" class="reset" value="RESET"/><BR/>
</form>
<div id="result">N/A</div>

i would like to set the value like this:

  1. If yes gets 3, result will be "Low"
  2. If yes gets 2 score, result will be "Medium"
  3. If no gets 3 or 2 score, result will be "High"
  4. No check for any radio button will show "N/A"
  5. 1 score for each yes and no will also show "N/A"

and I've tried my jquery script like this:

function scoring(){
        var score_yes = 0;
        var score_no = 0;
        $(".yes:checked").each(function()){
            score_yes += parseInt ($(this).val());
        });

        $(".no:checked").each(function(){
            score_no += parseInt ($(this).val());
        });

        if($(score_yes)==3){
            $("#result").html('LOW');
        }else if($(score_yes)==2){
            $("#result").html('MEDIUM');
        }else if(($(score_no)==3){
            $("#result").html('HIGH');
        }else if($(score_no)==2){
            $("#result").html('HIGH');
        }else{
            $("#result").html('N/A');
        }
    }
$(document).ready(function(){
    $(".yes:checked").change(function(){
        scoring();
    });
 });

but it didn't work. What should I fix?

Was it helpful?

Solution

There were many issues from extra parenthesis in a couple places to wrapping an int in a jquery object ($(score_yes) for example) to binding on .yes:checked (I believe you want .yes, .no as both those would change the #result). There was also a bit of simplification you could do. I believe this is what you want:

fiddle

function scoring() {
    var score_yes = $(".yes:checked").size();
    var score_no = $(".no:checked").size();

    if (score_yes == 3) {
        $("#result").html('LOW');
    } else if (score_yes == 2) {
        $("#result").html('MEDIUM');
    } else if (score_no == 3) {
        $("#result").html('HIGH');
    } else if (score_no == 2) {
        $("#result").html('HIGH');
    } else {
        $("#result").html('N/A');
    }
}
$(document).ready(function () {
    $(".yes, .no").change(function () {
        scoring();
    });
});

OTHER TIPS

Some syntax errors and score_yes should not be wrapped with jQuery

function scoring(){
        var score_yes = 0;
        var score_no = 0;
        $(".yes:checked").each(function(){
            score_yes += parseInt(this.value);
        });

        $(".no:checked").each(function(){
            score_no += parseInt(this.value);
        });

        if(score_yes==3){
            $("#result").html('LOW');
        }else if(score_yes==2){
            $("#result").html('MEDIUM');
        }else if(score_no==3){
            $("#result").html('HIGH');
        }else if(score_no==2){
            $("#result").html('HIGH');
        }else{
            $("#result").html('N/A');
        }
    }
$(document).ready(function(){
    $(".yes").change(function(){
        scoring();
    });
 });

DEMO

  function scoring(){
    var score_yes = 0;
    var score_no = 0;
    score_yes = $(".yes:checked").size();

    score_no = $(".no:checked").size();

   if(score_yes==3){
        $("#result").html('LOW');
    }else if(score_yes==2){
        $("#result").html('MEDIUM');
    }else if(score_no==3){
        $("#result").html('HIGH');
    }else if(score_no==2){
        $("#result").html('HIGH');
    }else{
        $("#result").html('N/A');
    }
}
$(document).ready(function(){
   $("#dform input:radio").change(function(){
    scoring();
   });
});

You can do that a lot simpler:

function scoring() {
    var score_yes = $("input.yes:checked").length;
    var score_no = $("input.no:checked").length;

    if (score_no >= 2)
        return 'HIGH';
    else if (score_yes == 3)
        return 'LOW';
    else if (score_yes == 2)
        return 'MEDIUM';
    else
        return 'N/A';
}

$(document).ready(function(){
    $('input[type="radio"]').change(function(){
        scoring();
    });
});

My own suggestion would be:

/* caching the radio inputs, to save time later
   (assuming there's a static collection): 
*/
var radios = $('input[type="radio"]');

// setting up the event-handler:
radios.change(function(){
    // filtering for how many '.yes' inputs are checked:
    var yes = radios.filter('.yes:checked').length,
    // filtering for how many '.no' inputs are checked:
        no = radios.filter('.no:checked').length;

    // setting the text of the '#result' element:        
    $('#result').text(function(){
        /* assuming a binary decision, then only, at most,
           half of the total number of elements can be checked, if they are
           all choices have been made:
        */
        if (yes + no === (radios.length)/2) {
            /* if 'yes' scores more than 1 (so either 2 or 3) we return 'Low',
               otherwise 'High':
            */
            return yes > 1 ? 'Low' : 'High';
        }
        // some are unchecked, therefore we return 'N/A':
        else {
            return 'N/A';
        }
    });
// triggering the 'change' event on DOM-ready, in case of default-checked radio inputs:
}).change();

JS Fiddle demo.

References:

You had multiple syntax errors in your code where you had extra { or ( where they were not needed. Also your main problem was that the selector for your change listener, $(".yes:checked"), only selected radio buttons that were already checked. You could change this by using $("input[type='radio']") as your selector. The full example you had can be found below

DEMO

$(document).ready(function () {
    $("input[type='radio']").change(function () {
        var score_yes = 0;
        var score_no = 0;
        $(".yes:checked").each(function () {
            score_yes += parseInt($(this).val());
        });

        $(".no:checked").each(function () {
            score_no += parseInt($(this).val());
        });

        if (score_yes === 3) {
            $("#result").html('LOW');
        } else if (score_yes === 2) {
            $("#result").html('MEDIUM');
        } else if (score_no === 3) {
            $("#result").html('HIGH');
        } else if (score_no === 2) {
            $("#result").html('HIGH');
        } else {
            $("#result").html('N/A');
        }
        });
    });

There is an extra parenthesis:

$(".yes:checked").each(function() ) {

Should be

$(".yes:checked").each(function(){
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top