How to add a style to an object property

TomDev

I'm a JS learner. I managed to access an object property but I would like to style it, for example - change its color.

const houseForSale = {
    material: "brick",
    size: "4 bedrooms",
    condition: "excellent",
    characteristic: "red roof"
}

const message = document.querySelector("#demo");

let styleFragment = houseForSale.characteristic;
styleFragment.style.color = "red";

let text = "The house has a " + styleFragment + ".";
console.log(text);
<p id="demo"></p>

When I do that, I get an error message: "can't access property 'color', styleFragment.style is undefined.

CertainPerformance

Text nodes (and plain strings) are not styleable - they don't have a .style property. Either create a string of HTML markup as well as, say, a <span> with a style:

const houseForSale = {
  material: "brick",
  size: "4 bedrooms",
  condition: "excellent",
  characteristic: "red roof"
};

const text = "The house has a <span style='color: red'>" + houseForSale.characteristic + "<span>.";
document.querySelector("#demo").innerHTML = text;
<p id="demo"></p>

Or explicitly create an element first, then assign to its style property.

const houseForSale = {
  material: "brick",
  size: "4 bedrooms",
  condition: "excellent",
  characteristic: "red roof"
};

const p = document.querySelector("#demo");

p.appendChild(document.createTextNode('The house has a '));

const span = p.appendChild(document.createElement('span'));
span.textContent = houseForSale.characteristic;
span.style.color = 'red';

p.appendChild(document.createTextNode('.'));
<p id="demo"></p>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add an object as a property of a JsonObject object?

How to add a property at the beginning of an object in javascript

How to add css box-shadow using .style JavaScript property

Javascript: How to add array values to object property

How to add new property to a Realm Object?

How to add custom property of object using .map

How do I add a property on a property of an object?

How to add new property to an object in cloud firestore

How do I refer to an object before the property "style.backgroundcolor"?

how to add a css style property dynamically in JSF

How to add a property in a string to an object in javascript?

How to Add to an unknown List property of an object at runtime?

How do I add an object property to the object's attributes?

how to add property in object in increasing order in javascript?

What happens if you add a property to a function using the object style?

How to add prototype property to existing object

How to add a property to a json object on the fly?

How to add property to an object?

how to add property to property to an object based on the condition in javascript?

How to add extra style besides styles object for one element?

how to add an additional property in the object that is assigned to a formControlName

Add a new property to an object property

How to add property into object in Node JS

How to add the property of same occurrence of object in an array?

How to add property on an object power apps

How to add a property to my object using a map

How to add array of strings into a property in an array of object?

How to add aditional object property in array of objects?

How to add a table column for an object property?