1

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.

4
  • Note, you should not mix approaches. If you are using jQuery, don't do document..... Keep approach consistent. Commented Aug 3, 2016 at 6:05
  • Can you please make this correct in that same plunkr link. Commented Aug 3, 2016 at 6:09
  • The issue is in script when appending the radio button the id you have given to it is id="answer_type0" but while validating you are looping the id's for radio button also. Commented Aug 3, 2016 at 6:16
  • @shubhamkhandelwal : Can you make this correct? Commented Aug 3, 2016 at 6:17

1 Answer 1

1

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

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

Comments

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.