0

I'm trying to add a class to an existing div that has multiple classes but doesn't work, the class is not added. I've added this on header (it's s shopify template):

<script type="text/javascript">
 jQuery(document).ready(function($) {
$('div.wk_right_header_inner wk_font_weight_bold wk_font_family wk_paddingbottom10').addClass('new-class');
 });
 </script>

The div looks like this (with no space after <):

<div class="wk_right_header_inner wk_font_weight_bold wk_font_family  
 wk_paddingbottom10"> Test </div>

Thanks

3
  • your selector syntax is wrong. And there's no need to use all the classes $('div.wk_right_header_inner').addClass('new-class'); }); is quite enough. Commented Sep 19, 2015 at 12:28
  • Yes, just pick one class name and addClass to that. Like: $('div .wk_right_header_inner').addClass('new-class'); Commented Sep 19, 2015 at 12:29
  • Wouldn't it be easier to give it an ID and use $('#id').addClass('new-class')? Commented Sep 19, 2015 at 12:30

2 Answers 2

2

You need to do

$('div.wk_right_header_inner.wk_font_weight_bold.wk_font_family.wk_paddingbottom10').addClass('new-class');
 });

Because now it thinks the other classes are sub elements.

Sign up to request clarification or add additional context in comments.

Comments

1

Have a look a this:

 jQuery(document).ready(function($) {
$(' .wk_right_header_inner ').addClass('new-class');
 });
.new-class
{
  color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="wk_right_header_inner wk_font_weight_bold wk_font_family wk_paddingbottom10"> Test </div>

1 Comment

@johnstoe Glad that it helped you!

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.