0

How do I change the below javascript to change my css class, rather than add/append to it?

<script type="text/javascript">
    (function($){   
        $(document).ready(function(){
        $('ul#horizontal.nav.menu').addClass('myNewClass');
        });
    })(jQuery);
</script>

EDIT

I am trying to change the following UL:

<ul class="nav menu" id="horizontal"> 

to

<ul class="myclass" id="horizontal">

I have tried the following just before my closing </body> tag and not working:

<script type="text/javascript">
        (function($){   
          $(document).ready(function(){
            // add items for bootstrap dropdown's
              $("#horizontal").attr("class", "myclass");
            })(jQuery);
        </script>
user991830
  • 864
  • 5
  • 17
  • 35
  • 1
    Right in the documentation for removeClass: http://api.jquery.com/removeClass/ – David Fleeman Nov 01 '13 at 13:22
  • @DhavalMarthak that is what he has now, in a closure to ensure the `$` variable within the function will not conflict with any other library – Rory McCrossan Nov 01 '13 at 13:22
  • 1
    Duplicate of [jquery change class name](http://stackoverflow.com/questions/3452778/jquery-change-class-name) – loler Nov 01 '13 at 13:23
  • 1
    id's are meant to be unique.. no need of making it more narrow by giving classnames like `.nav` or `.menu`. or prefixing with `ul`.. I may be wrong but I don't recommend it. Then you should do like this `document.getElementById('horizontal').className = "myNewClass";` – Mr_Green Nov 01 '13 at 13:24
  • I have tried all of these & added script just before my closing

    tag & they don't work. I am trying to change

    – user991830 Nov 01 '13 at 13:36

3 Answers3

2

You can try to use attr method:

$('ul#horizontal.nav.menu').attr( 'class', 'myNewClass');
antyrat
  • 27,479
  • 9
  • 75
  • 76
1

Quite easy..

$("#mydiv").attr("class", "myclass");

Robert de Jonge
  • 119
  • 3
  • 12
0
$("#mydiv").prop("class", "myclass");
Alex Coroza
  • 1,747
  • 3
  • 23
  • 39