add property to element javascript code example

Example 1: javascript change attribute

var element = document.getElementById("elemId");
element.setAttribute("attributeName", "value");

Example 2: how to add property to object in javascript

var data = {
    'PropertyA': 1,
    'PropertyB': 2,
    'PropertyC': 3
};

data["PropertyD"] = 4;

// dialog box with 4 in it
alert(data.PropertyD);
alert(data["PropertyD"]);

Example 3: set attribute javascript

Element.setAttribute(name, value);

Example 4: add property with value in js

var myObject = {
    sProp: 'some string value',
    numProp: 2,
    bProp: false
};