0

I want to remove and add required attribute to a field according to some conditions using jQuery. This changes in DOM, but it doesn't change visually.

My code

$('#id').removeAttr('required');
$("#id").attr('ng-required','true');
$("#id").attr('required','true');

Even used .prop(), but nothing seemed to work.

Gass
  • 7,536
  • 3
  • 37
  • 41
g.vihnga
  • 11
  • 5
  • do you try `attr.$observe('required', ...)` in your directive? – plong0 Aug 18 '16 at 05:36
  • @plong0,No I haven't used that.Can you please explain me how to use it.I have no idea how to use this cause I'm new to development.Thank You – g.vihnga Aug 18 '16 at 05:47
  • Possible duplicate of [How to set HTML5 required attribute in Javascript?](http://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript) – Rajeesh Menoth Aug 18 '16 at 05:53
  • No @rajeeshmenoth .My issue is it is not changing visually – g.vihnga Aug 18 '16 at 05:58
  • What is controlling the visual aspect of the element (is it in an angular directive, plain html)? What changes are you expecting to happen? – plong0 Aug 18 '16 at 14:18

4 Answers4

0

Try this for adding and removing required attribute. Value of required attribute is required not true

$('#email').removeAttr('required');
$("#email").attr('required','required');
$("#email").attr('ng-required','required');
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
0

Instead of 'true' use true

Try this code :

$('#id').removeAttr('required');
$('#id').prop('required',true);
Rajeesh Menoth
  • 1,704
  • 3
  • 17
  • 33
0
$('#id').removeAttr('required');

Should be working, are you sure you are targeting the proper element?

Also the .prop should be used with 'false' not true

Sptfr
  • 29
  • 4
0

Try using your jQuery like this:

$("#id").attr("required", false);
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83