KVK
KVK

Reputation: 1289

function not working in jsp page?

<script>
function KeepCount() {

    var x=0;
    var count=0;
    var x;
            for(x=0; x<document.QuestionGenerate.elements["questions"].length; x++){

            if(document.QuestionGenerate.elements["questions"][x].checked==true || document.QuestionGenerate.elements["option"][x].checked==true || document.QuestionGenerate.elements["Description"][x].checked==true || document.QuestionGenerate.elements["fillups"][x].checked==true){

                   count= count+1;
                   document.getElementsByName("t1")[0].value=count;    
                }
                else
                    {

                     document.getElementsByName("t1")[0].value=count;

                     //var vn=$('#t1').val();
                   // alert(vn);

                    //alert(vn);
                      //alert("value is"+count);
                    }
            }

           // var cc = document.getElementsByName("t1")[0].value;

            var vn=$('#t1').val();
            alert(vn);

            if(vn==0){
                alert("You must choose at least 1");
               return false;
            }
        }




</script>

<form action="SelectedQuestions.jsp" method="post" name="QuestionGenerate">



  <input type="text" name="t1" id="t1" value="">
<input type="submit" id="fi" name="s" value="Finish" onclick="return KeepCount();">
</form>

I use the above code for checking how many check box are checked in my form my form having many check box. and if no check box are selected means it shows some message and than submit the form but for loop is working good and textbox get the value after the for loop the bellow code doesn't work even alert() is not working

**

var vn=$('#t1').val();
                alert(vn);

                if(vn==0){
                    alert("You must choose at least 1");
                   return false;
                }

This code is not working why?

**

Upvotes: 1

Views: 416

Answers (2)

KVK
KVK

Reputation: 1289

I change my KeepCount() function code shown in bellow that solve my problem

 function KeepCount()
  {


          var check=$("input:checkbox:checked").length;
          alert(check);

          if(check==0)
              {
              alert("You must choose at least 1");
              }
    return false;

  }

Upvotes: 1

Prakhar Asthana
Prakhar Asthana

Reputation: 91

The bug is : document.QuestionGenerate.elements["questions"] it is undefined that's why the code is not even going inside for loop use instead :

document.QuestionGenerate.elements.length

Upvotes: 0

Related Questions