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>
5
  • 1
    Right in the documentation for removeClass: api.jquery.com/removeClass Commented Nov 1, 2013 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 Commented Nov 1, 2013 at 13:22
  • 1
    Duplicate of jquery change class name Commented Nov 1, 2013 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"; Commented Nov 1, 2013 at 13:24
  • I have tried all of these & added script just before my closing </body> tag & they don't work. I am trying to change <ul class="nav menu" id="horizontal"> to <ul class="myclass" id="horizontal"> Commented Nov 1, 2013 at 13:36

3 Answers 3

2

You can try to use attr method:

$('ul#horizontal.nav.menu').attr( 'class', 'myNewClass');
Sign up to request clarification or add additional context in comments.

Comments

1

Quite easy..

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

Comments

0
$("#mydiv").prop("class", "myclass");

1 Comment

You should use className instead if you use prop method and not attr.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.