Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JS can't set style property

Ok let’s look at this part of code

  $(tbody).find('tr').each((i, oldTbodyTr) => {
     newTr = document.createElement('tr');
     $(oldTbodyTr).find('td').each((i, oldTd) => {
       let newTd = document.createElement('td');
       newTd.innerHTML = oldTd.innerHTML;
       newTd.classList = oldTd.classList;
       newTd.style = oldTd.style; //Doesn't work
       newTr.appendChild(newTd);
     });
   newTableTbody.appendChild(newTr);
 });

It creates a new table body by looping through all rows in tbody of an already existing table.
Everything is fine, except that the style of old td doesn’t transfer to new td element.
I can’t figure out why.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you want to copy inline styles, you could try newTd.style.cssText = oldTd.style.cssText since style itself is immutable.

See more about style.cssText.

const source = document.getElementById('source');
const target = document.getElementById('target');

target.style.cssText = source.style.cssText;
<p id="source" style="color: red; background-color: blue; font-size: 20px;">Source</p>
<p id="target">Target</p>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading