Javascript question

Hi there,

I’m struggling with the following:
I want to add a class name to another class so I can add something when the checkbox in my panel is checked. I have the following. The checkbox works (tested with some H1) but my Javascript don’t work. Can anybody help me?

Thanks!

<?php if($page->checkboxField()->bool()) :?>
<script type="text/javascript">
  document.getElementsByClassName("text").classList.add("read-under-image-help-text");
</script>
<?php endif ?>

Where are you using that code?

Hi! Thanks for your reaction.

I use it as part of my ‘project’ template.

The problem is not your Kirby code or in any way Kirby related, but your JavaScript. getElementsByClassName()doesn’t return a single element by a collection of nodes, so you can’t just string classList.add()

I.e. you either have to grab a single element by its index or loop through the node list.

1 Like

Thanks for your reply.

Unfortunately, I did not fix it with JavaScript. I made a workaround with inline styling.

Thanks anyway! :slight_smile: