Search code examples
jqueryhtmlrequired

Get required fields and check if true or false


I need to change an application which sets inputs to required="true" or required="false". My problem here is when I try to get all of inputs which are required, I just get all of them even the false one.

What I tried:

$('input[required=true') -> gets all of them

var $input = $('input');
$.each($input, function(sKey, sValue){
    if($('#' + sValue.id).prop('required') == "true"){
        //Gets all even the false one
    }

    if($('#' + sValue.id).prop('required')){
        //Gets all even the false one
    }

    if($('#' + sValue.id).attr('required') == "true"){
        //Gets all even the false one
    }

    if($('#' + sValue.id).attr('required') == "required"){
        //Gets all even the false one
    }

    if($('#' + sValue.id).attr('required') == "required"){
        //Gets all even the false one
    }

});  

What am I doing wrong?


Solution

  • Try like following.

       var input = $('input');
    
        $.each(input, function (sKey, sValue) {
            if (sValue.outerHTML.contains('required="true"')) {
                //Gets all true
            }
        })