0

I want to add an additional class to an input. I have no access to the HTML to change the code.

I tried the below. I don't really know JS, but have to get this done for work.

<script type="text/javascript">
    function changeClass(){
        document.getElementById("en__field_supporter_emailAddress").className += " xverify_email";
    }

    window.onload = function(){
        document.getElementsByTagName("button").addEventListener( 'click', changeClass);
    }
</script>

I want the JS to insert the "xverify_email" class into the email address input line (which has the id en__field_supporter_emailAddress and also already has a class that must remain there) so that it can call the subsequent xverify JS to work.

Ele
  • 33,468
  • 7
  • 37
  • 75

1 Answers1

0

I'm assuming you have a set of buttons, so you want to bind the event click to every button in the page:

Array.from(document.querySelectorALl('button')).forEach(function(btn) {
    btn.addEventListener('click', changeClass);
});

I recommend you to embrace the attribute classList:

function changeClass() {
    document.getElementById("en__field_supporter_emailAddress").classList.add("xverify_email");
}
Ele
  • 33,468
  • 7
  • 37
  • 75