0

I want to dynamically changing some values in grid-area property using JS.

var d = ["about", "contact"];
  document.getElementById("container").style.gridTemplateAreas = '"services d[0] d[1] content content content"';

Is it possible to place for example d[0] inside these signs ' " to make my website more dynamic? The layout is not changing because JS reads it as a string "d[0]" not as an assigned value, which is "about". I'm going to randomly assigning values from the array, but for this moment I just want to place a specific value.

2 Answers 2

3

yes, you can simply do that using Template literal

var d = ["about", "contact"];
  document.getElementById("container").style.gridTemplateAreas = `services ${d[0]} ${d[1]} content content content`;
  1. use `` instead of ""
  2. then place your variable inside ${}

Example :

var names = ["Adam","Sara"]

   console.log(`welcome mr.${names[0]} and ms.${names[1]}, Have a nice day :)`)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I will remember it for other projects, but it doesn't work in my case. I guess gridTemplateAreas requires extra ' ' signs ? When I tried without placing array value just writing document.getElementById("container").style.gridTemplateAreas = ' "services about content content content content" '; - the layout has changed, but without extra ' ' , the layout hasn't
0

Thx Mohammed for the tip. To make it work, I've changed document.getElementById("container").style.gridTemplateAreas = `services ${d[0]} ${d[1]} content content content`; into document.getElementById("container").style.cssText = `grid-template-areas: 'services ${d[0]} content content content content';`; Great

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.