Pattu Pattu - 1 month ago 7
HTML Question

JQuery check if the inout is required or not

In my HTML template, I have required set to False for a specific input.

<div class="col-xs-2">
<input type="text" required="false" class="product-data form-control" id="description" placeholder="Description" name="description"/>
</div>


I want to print the value of required in JQuery.

console.log("Value - " + $(this).attr("required"));


Output :


Value - required


I want it to print false. Is there any way to do this.

Answer

Try to use getAttribute() :

document.getElementById('description').getAttribute('required')

Hope this helps.

console.log("Value - " + document.getElementById('description').getAttribute('required'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-xs-2">
    <input type="text" required="false" class="product-data form-control" id="description" placeholder="Description" name="description"/>
</div>