0

I am having 5 checkboxes,when i click each check box percentage should be increased similarly when i uncheck the checkbox percentage should be decreased.

$(document).ready(function(){
        var arr=['c','f','i','n','s'];
        var n=6;
            $("input").click(function(){
            if(this.checked){
            if($.inArray(this.value,arr)>-1){
                for(var count=1;count<n;count++)
                { 

                  var percentage=Math.ceil(count * (100/5));
                  alert('your percentage is' + percentage);
                }
            }else{}}
            });

});

But it is returning 100% for a single click.What i am doing wrong?

6
  • show html code also or add plunker Commented Dec 29, 2016 at 6:53
  • jsfiddle.net/x4bb87L4 Commented Dec 29, 2016 at 6:57
  • why are you using checkboxes to do what radios are intended to be used for? Commented Dec 29, 2016 at 7:03
  • @charlietfl Nothing i am just practicing examples to learn jquery Commented Dec 29, 2016 at 7:11
  • 1
    check the answer provided by me , should work perfectly for you Commented Dec 29, 2016 at 8:26

2 Answers 2

1

You can check updated fiddle jsfiddle.net/x4bb87L4/3/

Code that updated

    $(document).ready(function(){       
      var arr=['c','f','i','n','s']; 

      $("input").click(function(){
        $(this).siblings('input[type="checkbox"]').not(this).prop('checked', false).next().removeClass("fa-times cross fa-check tick");

        if(this.checked){
            if($.inArray(this.value,arr)>-1){                                                   
                $(this).next().addClass("fa-check tick");           
            }else{                                                        
               $(this).next().addClass("fa-times cross");           
            }                 
        }else{
            $(this).next().removeClass("fa-check times tick cross");
        }

        var correctAnswer=$(".fa-check.tick").length;
        //var wrongAnswer=$(".fa-times.cross").length;
        //var count=correctAnswer-wrongAnswer;
        var count=correctAnswer;
        count=count<0?0:count;
        $('#value').html(count);
        var percentage=Math.ceil(count * (100/5));
        percentage=percentage<0? 0 :percentage;
        $('#percent').html(percentage);

    });             
 });
Sign up to request clarification or add additional context in comments.

5 Comments

Have you seen above fiddle? Is it possible to decrease percentage when i click wrong answer?i.e i have to allocate only 20 percent per row .when i click correct answer percentage should be increased when i click wrong answer percentage should be decreased
yes I have updated fiddle code. Check it jsfiddle.net/x4bb87L4/2
I am getting wrong answer:-( i said i need to allocate only 20 percent per row,But it checks all check boxes.Can you please check again?
Yes let me check it
I have updated logic as per you told. You can check it here jsfiddle.net/x4bb87L4/3
1

According to me this should be done by creating an other array like

var n=0;
  var arrayforselection=[];
        $("input").click(function(){
        if(this.checked){

  if($.inArray(this.value,arrayforselection)<0){
  if($.inArray(this.value,arr)>-1){
  arrayforselection.push(this.value);
  }
  }
  }else{

  if($.inArray(this.value,arrayforselection)>=0){
  if($.inArray(this.value,arr)>-1){
  arrayforselection.indexOf(this.value)
  arrayforselection.splice(arrayforselection.indexOf(this.value),1);
  }
  }
  }
  n=arrayforselection.length;
        if($.inArray(this.value,arr)>-1){
  if(n===0){
             $('#value').html(n)
              var percentage=Math.ceil(n * (100/5));
              $('#percent').html(percentage)

  }
            for(var count=1;count<=n;count++)
            {
              $('#value').html(count)
              var percentage=Math.ceil(count * (100/5));
              $('#percent').html(percentage)
            }
        }else{

  }
        });

the previous selected Answer will not work in you multiple times check and unchecked the correct answer , it will increase the percentage and will not decrease it . Find the Updated Jsfiddel here

1 Comment

Ok i will check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.