Answers for "add attribute to object javascript"

7

javascript add attribute

var element = document.getElementById('btnBuild');
element.setAttribute("disabled", "disabled");
Posted by: Guest on June-23-2021
2

add attribute to element

//This is for creating an attribute with setAttributeNode
<script>
var f = document.querySelector("#menu-item-16 > a");// Get the <a> element
var c = document.createAttribute("target");// Create a "target" attribute
c.value = "_blank"; // Set the value of the target attribute
f.setAttributeNode(c); // Add the target attribute to <a>
</script>

//With setAttribute
<script>
let e = document.querySelector("#menu-item-16 > a");
e.setAttribute("target", "_blank");
</script>
Posted by: Guest on September-19-2021
14

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"]);
Posted by: Guest on April-16-2020
2

Add New Properties to a JavaScript Object

var myDog = {
  "name": "Happy Coder",
  "legs": 4,
  "tails": 1,
  "friends": ["freeCodeCamp Campers"]
};

myDog.bark = "woof";
// or
myDog["bark"] = "woof";
Posted by: Guest on July-19-2020

Code answers related to "add attribute to object javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language