250

I have been searching ways to have jQuery automatically write required using html5 validation to my all of my input fields but I am having trouble telling it where to write it.

I want to take this

 <input type="text" name="first_name" value="" id="freeform_first_name"
 maxlength="150">

and have it automatically add required before the closing tag

 <input type="text" name="first_name" value="" id="freeform_first_name"
 maxlength="150" required>

I thought I could do someting along the lines of

$("input").attr("required", "true");

But it doesn't work. Any help is greatly appreciated.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Miura-shi
  • 4,409
  • 5
  • 36
  • 55

6 Answers6

552
$("input").prop('required',true);

DEMO FIDDLE

Kiran
  • 20,167
  • 11
  • 67
  • 99
71

You can do it by using attr, the mistake that you made is that you put the true inside quotes. instead of that try this:

$("input").attr("required", true);
Joz Naveen Joz
  • 837
  • 7
  • 3
  • 2
    @JohnMeyer "input" is the name of a tag selector – dave4jr Feb 11 '17 at 23:09
  • `required` is a _boolean_ attribute and should only ever be omitted (for "false"), or have the same value as its name (i.e. `"required"`) for "true". It's actually better to use `.prop()`. – Alnitak Jan 10 '20 at 10:33
  • @Alnitak Not if you're working on making a site compatible with IE, `prop()` doesn't work on there as it does on modern browsers. Still best to use `$(ele).attr("required", true)` and `false` to remove. – Oliver Heward Jun 18 '20 at 15:09
46

I have found that the following implementations are effective:

$('#freeform_first_name').removeAttr('required');

$('#freeform_first_name').attr('required', 'required');

These commands (attr, removeAttr, prop) behave differently depending on the version of JQuery you are using. Please reference the documentation here: https://api.jquery.com/attr/

John Meyer
  • 2,296
  • 1
  • 31
  • 39
19

Using .attr method

// syntax
.attr(attribute,value);

.attr("required", true);
// output: required="required"

.attr("required", false);
// output:

Using .prop

// syntax
.prop(property,value)

.prop("required", true);
// output: required=""

.prop("required", false);
// output: 

Read more from here

https://stackoverflow.com/a/5876747/5413283

Dexter
  • 7,911
  • 4
  • 41
  • 40
8

Should not enclose true with double quote " " it should be like

$(document).ready(function() {            
   $('input').attr('required', true);   
});

Also you can use prop

jQuery(document).ready(function() {            
   $('input').prop('required', true);   
}); 

Instead of true you can try required. Such as

$('input').prop('required', 'required');
Rokan Nashp
  • 331
  • 4
  • 13
6

I found that jquery 1.11.1 does not do this reliably.

I used $('#estimate').attr('required', true) and $('#estimate').removeAttr('required').

Removing required was not reliable. It would sometimes leave the required attribute without value. Since required is a boolean attibute, its mere presence, without value, is seen by the browser as true.

This bug was intermittent, and I got tired of messing with it. Switched to document.getElementById("estimate").required = true and document.getElementById("estimate").required = false.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93