F
F
Felino2020-01-20 12:33:15
css
Felino, 2020-01-20 12:33:15

Passing parameters to css from Json?

Good afternoon!
Please tell me, I am reading width, height parameters from JSON.
How can I pass these parameters to CSS?
example:

{
  "global": [
    {
      "id": "1",
      "width": "50",
      "height": "150"
    },
    {
      "id": "2",
      "width": "150",
      "height": "350"
    },
    {
      "id": "3",
      "width": "750",
      "height": "850"
    }
  ]
}

How to put what I thought in CSS
.Layer + id{
    width:   px; /* Ширина блока вставить из того что я считал */   
    height:   px; /* Высота блока вставить из того что я считал*/   
   }

either in html, instead of spaces, what I read from the file
<button style="height: px;width: px"></button>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
fallus, 2020-01-20
@fallus

const all = {
  "global": [
    {
      "id": "1",
      "width": "50",
      "height": "150"
    },
    {
      "id": "2",
      "width": "150",
      "height": "350"
    },
    {
      "id": "3",
      "width": "750",
      "height": "850"
    }
  ]
};

for(item of all.global){
    let elm = document.querySelector('.layer' + item.id);
    elm.style.width = item.width + 'px';
    elm.style.height = item.height + 'px';
}

I
iddqda, 2020-01-20
@iddqda

the first way is not sure what is possible,
but dynamically I would try something like this:

jstr.global.forEach((a) => 
   document.querySelectorAll(".Layer + a.id").forEach((el) => 
        { el.style.width  = a.width; el.style.height = a.height }))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question