Question

I am trying to add a class to a dynamically generated menu using jQuery. I am building the site on Business Catalyst so I do not have access to the server side code. I tried using the .addClass() method to add a class to my ul item. I am doing this because I am using Twitter Bootstrap and Business Catalyst generates ul menus with a unique id and not a class. The code I tried does not appear to add a class to the ul element in my menu. Any help would be appreciated.

Code:

    <div class="navbar">
    <div class="navbar-inner">
        <div class="container">
          {module_menu,14623}
        </div>
    </div>

<script type="text/javascript">
$(document).ready(function(){
    $("#nav_14623").addClass("nav");
});
</script>

Website URL: http://jshub.designangler.com/

Was it helpful?

Solution

your script code is being added before the jquery code is included you have to put your script code after it

Currenttly looks like:

<script type="text/javascript">
    $(document).ready(function(){
        $("ul#nav_14623").addClass("nav");
    });
</script>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="/js/bootstrap.js" /></script>

Needs to be

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="/js/bootstrap.js" /></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("ul#nav_14623").addClass("nav");
    });
</script>

OTHER TIPS

Run this to test whether your have jQuery first:

<script>
$(function(){

alert('jQuery ok!')
})

</script>

If you get an alert, then jQuery is there if not, then no jQuery is installed or not properly set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top