0

I am attempting to have the class changed on HTML "a" content on JS function success. However, my code to change the class by id is not working. I cant seem to figure out what I am doing wrong or if I am missing something.

Scenario: Three "a" tags create fancybox buttons, when a file is loaded successfully, they outline in green(css is already written and verified to be good - "success" keyword in class) so the user knows selections are good to go.

HTML code for tags

JS code that should change their class contents but isnt -

document.getElementById('misc-selection-button').class = 'button big success square fit mr-4';
document.getElementById('weapon-selection-button').class = 'button big success square fit mr-4';
document.getElementById('target-selection-button').class = 'button big success square fit mr-4';

console.log("misc-selection-button class:" + document.getElementById('misc-selection-button').class);
console.log("weapon-selection-button class:" + document.getElementById('weapon-selection-button').class);
console.log("target-selection-button class:" + document.getElementById('target-selection-button').class);
<div class="row justify-content-center mt-5">
    <div class="col-5">
        <a data-fancybox href="#load-miscellaneous" id="misc-selection-button" class="button big square fit mr-4">Miscellaneous</a>
    </div>
</div>


<div class="row justify-content-center mt-5">
    <div class="col-5">
        <a data-fancybox href="#load-weapon" id="weapon-selection-button" class="button big square fit mr-4">Weapon</a>
    </div>
    <div class="col-5">
        <a data-fancybox href="#load-target" id="target-selection-button" class="button big square fit ml-4">Target</a>
    </div>
</div>

5
  • Use className or classList Commented Oct 21, 2020 at 19:37
  • Code seems to work? Commented Oct 21, 2020 at 19:38
  • Thank you! That was the issue. className made it successful. Commented Oct 21, 2020 at 19:39
  • I suggest you to play with classList instead, with classList.toggle('success', true) Commented Oct 21, 2020 at 19:41
  • 1
    @ChipBrommer if you have found a solution don't forget to mark the accepted answer Commented Oct 21, 2020 at 19:46

3 Answers 3

0

I think that your syntax isn't correct. It should be: document.getElementById('misc-selection-button').className = 'button big success square fit mr-4';

Sign up to request clarification or add additional context in comments.

Comments

0

This is probably a syntax problem try

document.getElementById("myDIV").className = "mystyle";

Comments

0

You have to do

// add a class to the list
document.getElementById('misc-selection-button').classList.add('success');
// remove a class to the list
document.getElementById('misc-selection-button').classList.remove('success');
// toggle class in the list
document.getElementById('misc-selection-button').classList.toggle('success', true);
// set all classes once (overwrite)
document.getElementById('misc-selection-button').className = "button big success square fit mr-4";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.