Dynamically add specific attr to element

kipris

I created valid-file attribute using angular directive because file-input (with bootstrap filestyle plugin) don't see required attribute :

    .directive('validFile',function(){
        return {
            require:'ngModel',
            link:function(scope,el,attrs,ngModel){
                //change event is fired when file is selected
                el.bind('change',function(){
                    scope.$apply(function(){
                        ngModel.$setViewValue(el.val());
                        ngModel.$render();
                    })
                })
            }
        }
    })

I need to dynamically add or remove valid-file attribute. If I use $("input").attr("valid-file"); nothing will change. If I use $("input").attr("valid-file",true); it will add attribute valid-file="true" but validation will break. Can you advice me something? Here's plunker http://plnkr.co/edit/Gcbb7r05VBTjbh9x5Ma8?p=preview

Jayesh Chitroda

.attr() need 2 parameters if you want to set any attribute in any element.

ex. $("input").attr("valid-file",""); // empty will also ok..or any value instead of empty

because only $("input").attr("valid-file") is used to get value of attribute "valid-file"

So you need 2 parameter in .attr() to set any attribute

Try this: http://plnkr.co/edit/lSl3GdZrRAyFps5QOSlw?p=preview

 $("select").change(function(){
    if ($("select option:selected").val() == "0") {
      $("input").attr("valid-file",""); // try this
    } else {
      $("input").removeAttr("valid-file");
    }
  });

HTML:

<select name="" id="">
  <option value="">Select</option>
  <option value="0">Add attr
  <option value="1">Delete attr
</select>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically add new element to specific list

From Dev

Dynamically add element to layout

From Dev

JAXB add element dynamically

From Dev

Dynamically add/remove element

From Dev

select specific element using jQuery attr() Method

From Dev

jquery attr add id to existing element

From Dev

Add multiple classes to an element using .attr?

From Dev

add all data attr from element in array

From Dev

Checking element has attr/class tagged dynamically or not in Angular2

From Dev

Add class to dynamically added element

From Dev

Dynamically add element to XML file

From Dev

Add element on top of image dynamically

From Dev

Dynamically add element in JS to XUL

From Dev

Dynamically add element using angularJS

From Dev

Add element on top of image dynamically

From Dev

dynamically add attribute to child element

From Dev

Vanilla JavaScript - Syntax for adding class to element with specific data attr

From Dev

How to find a spesific class,and find a spesific element and add disabled attr to it

From Dev

XSLT add element at specific position

From Dev

Add element to specific index of list

From Dev

How to dynamically add rows to a specific UITableView section?

From Dev

add a row to a specific table created dynamically by user

From Dev

How to add columns at a specific position dynamically?

From Dev

How to add CSS class dynamically to html element

From Dev

How to add dynamically extended element with Polymer?

From Dev

Create div element and dynamically add child elements on it

From Dev

Add data to dynamically created element and bind event

From Dev

check if span element exists and then add div dynamically

From Dev

How to dynamically add a directive to an input element in AngularJS

Related Related

  1. 1

    Dynamically add new element to specific list

  2. 2

    Dynamically add element to layout

  3. 3

    JAXB add element dynamically

  4. 4

    Dynamically add/remove element

  5. 5

    select specific element using jQuery attr() Method

  6. 6

    jquery attr add id to existing element

  7. 7

    Add multiple classes to an element using .attr?

  8. 8

    add all data attr from element in array

  9. 9

    Checking element has attr/class tagged dynamically or not in Angular2

  10. 10

    Add class to dynamically added element

  11. 11

    Dynamically add element to XML file

  12. 12

    Add element on top of image dynamically

  13. 13

    Dynamically add element in JS to XUL

  14. 14

    Dynamically add element using angularJS

  15. 15

    Add element on top of image dynamically

  16. 16

    dynamically add attribute to child element

  17. 17

    Vanilla JavaScript - Syntax for adding class to element with specific data attr

  18. 18

    How to find a spesific class,and find a spesific element and add disabled attr to it

  19. 19

    XSLT add element at specific position

  20. 20

    Add element to specific index of list

  21. 21

    How to dynamically add rows to a specific UITableView section?

  22. 22

    add a row to a specific table created dynamically by user

  23. 23

    How to add columns at a specific position dynamically?

  24. 24

    How to add CSS class dynamically to html element

  25. 25

    How to add dynamically extended element with Polymer?

  26. 26

    Create div element and dynamically add child elements on it

  27. 27

    Add data to dynamically created element and bind event

  28. 28

    check if span element exists and then add div dynamically

  29. 29

    How to dynamically add a directive to an input element in AngularJS

HotTag

Archive