Pergunta

I'm trying to set/add a class name to a div by targeting its ID but I'm not having much success at the minute.

I basically have my website navigation saved to an external file so all I need to do is make changes to one file to update all pages, but doing this restricts my website users from being able to easily identify which page of the site they are on.

I have a class="current" option that visually tells the user where they are in the menu but I can't for the life of me add the class to the navigation menu. I'm trying to run the code after the nav menu file is loaded in using the following code, where am I going wrong?

<?php include_once ("includes/page_top.php"); ?>
    <script type="text/javascript">
        document.getElementById('home').className("current");
    </script>
Foi útil?

Solução

className isn't a function.

Instead of this:

document.getElementById('home').className("current");

You want this:

document.getElementById('home').className = "current";
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top