Add div attributes value dynamically

setattribute is not a function
setattribute javascript
jquery add attribute
set attribute jquery
remove attribute javascript
javascript setattribute onclick
javascript add style to element
setattribute style

I want to add div attributes dynamically in Angular 7.

And I tried this:

<div *ngFor="let e of etats._embedded.etats" 
style="background: {{e.codeCouleur}} !important;"  
data-code="{{e.id}}" data-bg={{e.codeCouleur}}>{{e.nom}"</div>

And I have this error:

Uncaught Error: Template parse errors: Can't bind to 'code' since it isn't a known property of 'div'. (" *ngFor="let e of etats._embedded.etats" style="background: {{e.codeCouleur}} !important;" [ERROR ->]data-code="{{e.id}}" data-bg={{e.codeCouleur}}>{{e.nom}"

When you're looping dynamically you should make use of something like this:

<div *ngFor="let e of etats._embedded.etats" 
     [style.background]="e.codeCouleur"
     [data-code]="e.id" 
     [data-bg]=e.codeCouleur
>{{e.nom}}"</div>

DOM Element setAttribute() Method, If the specified attribute already exists, only the value is set/changed. Note: Although it is possible to add the style attribute with a value to an element with this  I suppose you want to add control to div dynamically. One suggestion is that you can add the id and runat attributes to the div and can add RadChart to the div. Following is the sample code. ASPX: <

You have few options:

  1. <div attributeName="{{attributeValue}}"/></div>
  2. <div [attr.attributeName]="attributeValue"/></div>
  3. <div [attr.attributeName]="getAttributeValueFun()"/></div>

Element.setAttribute(), Sets the value of an attribute on the specified element. If the attribute already exists, the value is updated; otherwise a new attribute is added  function addDiv(innertext) { var r = document.createElement('Div'); r.style.height="20px"; r.style.width="25px"; r.appendChild(innertext); } this will add a div on the page. If you want to add this in a specific control then use can use document.getElementById('specific control name').createElement('Div');

The thing you're trying to achieve is called Attribute Binding. As the official document says:

You must use attribute binding when there is no element property to bind.

So, according to that, you need to change the code a little bit. For style, you can use ngStyle or [style.background], but !important won't work in it:

<div *ngFor="let e of etats._embedded.etats" 
  [ngStyle]="{'background': e.codeCouleur}"  
  [attr.data-code]="e.id" [attr.data-bg]="e.codeCouleur">{{e.nom}}</div>

How to change style attribute of an element dynamically using , How to add/update an attribute to an HTML element using JavaScript? How to sort element by numerical value of data attribute using JavaScript ? How to remove “  Dynamically add/change attributes of values in C#. Ask Question Asked 2 years, 11 months ago. Active 2 years, 11 months ago. Viewed 14k times 3. I want to tag the

Selectors, The dynamic pseudo-classes E[foo~="warning"], Matches any E element whose "foo" attribute value is a list of Although the intention of these rules is to add emphasis to text by changing its color, the effect will be lost in a case such as​: matches a P element that is a grandchild or later descendant of a DIV element​. Taken from: Properties and Attributes in HTML. The value property reflects the current text-content inside the input box, whereas the value attribute contains the initial text-content of the value attribute from the HTML source code. So if you want to know what's currently inside the text-box, read the property.

Attributes and properties, For instance, let's create a new property in document.body : body > < div id = " elem " about = " Elephant " > </ div > < script > alert ( elem . getAttribute ( '​checked' ) ) ; // the attribute value is: empty string alert ( input . checked ) ; // the property value is: Set Property Dynamically via a Character String. Modifying Attributes. Attributes are values that contain additional information about HTML elements. They usually come in name/value pairs, and may be essential depending on the element. Some of the most common HTML attributes are the src attribute of an img tag, the href of an a tag, class, id, and style.

How to Add Attribute to an HTML Element in jQuery, In the following example when you click on the "Select Checkbox" button it will add the checked attribute to the checkbox dynamically using jQuery. Use element.style property to set the style attribute of an element. Set the properties either by using bracket notation or dash notation . Example 1: This example changing the color and background-color of heading element.

Comments
  • I'm using angular7
  • use this {{e.nom}} instead of {{e.nom}"
  • use [attr.data-code]="e.id" and [attr.data-bg]="e.codeCouleur" instead of data-code="{{e.id}}" data-bg="{{e.codeCouleur}}"
  • Thanks @CruelEngine, it's works. But how can i resolve the style="..." ?
  • [style.background]="e.codeCouleur"