satya
satya

Reputation: 3560

Can not validate properly the radio button using JavaScript/jQuery

I cannot check the radio button select in my validation using JavaScript/jQuery. I am generating some multiple textarea, radio button and drop down list dynamically. My requirement is I will fill all field and check the validation. But it's not happening properly.

Here is my code:

function validate(){
    var x =document.getElementById('ques').value;
    console.log('value',x);
    for(var i=0;i<x;i++){
        var tid="questions"+ i;
        var rid='answer_type'+i;
        var sid="nscale"+i;
        var scaleid='#scaleid'+i;
        if(document.getElementById("questions"+ i)){
            if(document.getElementById(tid).value==''){
                alert('Please enter the question');
                return false;
            }else if($('input[id=answer_type' + i + ']').is(':checked')==false){
                alert('Please check the answer type');
                return false;
            }else if($(scaleid).css('display') == 'block'){
                  if(document.getElementById(sid).value==''){
                    alert('Please select the scale');
                    return false;

                  }
            }else{

            }
        }
    }
}

My complete code is here plunkr. My problem is when user has selected all radio button and also click on validate button still its displaying the alert message as Please check the answer type. My requirement is I will check all field and drop down list whether these are blank or not in validation case.

Upvotes: 1

Views: 48

Answers (1)

shubham khandelwal
shubham khandelwal

Reputation: 181

Try and make the id of radio button in the addQuestionField() function as id="answer_type'+i+'" instead of id="answer_type0".

Upvotes: 1

Related Questions