Search code examples
javascriptelementitalic

How to change the background colors of all <em> elements


I am trying to change the background colors of all italized text, instead of using a span on every single word through the paragraph. It says <em> next to the italized text. I have tried

$(".em").css({
    "background-color":"#d9f9f9",
});

or/and tried this:

var elem=document.getElementByTagName(em)[1];
elem.style.backgroundColor='#d9f9f9';

Solution

  • You could try the following:

    const italics = document.querySelectorAll('em');
    
    italics.forEach(italic => {
      italic.style.backgroundColor = '#d9f9f9';
    });