Skip to main content

How to add custom attributes with special characters to elements through code?

Anyone knows how to add attributes to an element through custom code(I know it's possible)?

Use case would be: data-ms-content="!CMS_FIELD"
Basically need to have "!" in front of a field pulled from CMS,

2 comments

  • Michel Schade
    Michel Schade

    const element= document.querySelector("{Insert #id or .class");
    element.setAttribute("data-ms-content", "!"+{CMS_FIELD});

    0
  • Marko Guzvic
    Marko Guzvic OP

    I actually found this but forgot to send it here.

    <script> document.addEventListener('DOMContentLoaded', function() { var myElement = document.querySelector('.hide-content');
    myElement.setAttribute('data-ms-content', '!CMS_LINK');
    }); </script>
    0

Please sign in to leave a comment.

Sitemap