Adding attributes to an element through custom code Answered

Post author
Marko Guzvic

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,

Comments

2 comments

  • Comment author
    Michel Schade

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

    0
  • Comment author
    Marko Guzvic

    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.