3

I have some lists in my page which are the same

<ul class="tabItem">
    <li class="active">
        <hr>
        <a href="#tab-1">tab1</a>
        <hr>
    </li>
    <li>
        <hr>
        <a href="#tab-2">tab2</a>
        <hr>
    </li>
    ...
</ul>

How can I add css to all <ul>tags with .tabItem class at the whole page using jQuery?

Currently I'm doing this, which works only on the first <li> childs

$(".tabItem li").children('hr').eq(0).css({"transform": "translateY(-12px)"});
$(".tabItem li").children('hr').eq(1).css({"transform": "translateY(12px)"});
3
  • 3
    why not just use CSS? do you need to use jquery? Commented Feb 16, 2015 at 18:24
  • Just to confirm, you want to get the first two <li> in .tabItem? Commented Feb 16, 2015 at 18:26
  • @JacobGray Yes! I'm using this in .tabItem li:hover to add effects. And I need this to add to all hrs in the all <li> tags Commented Feb 16, 2015 at 18:26

4 Answers 4

6

Here's all ul tags with .tabItem class:

$('ul.tabItem').css({ });

Here's the child action you're attempting:

$('ul.tabItem li hr:first-child').css({"transform": "translateY(-12px)"});
$('ul.tabItem li hr:last-child').css({"transform": "translateY(12px)"});

Although just using CSS would be better:

ul.tabItem li hr:first-child {
    transform: translate(-12px);
}
ul.tabItem li hr:last-child {
    transform: translate(12px);
}

Using CSS method then there's no JavaScript necessary.

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

3 Comments

I need the jQuery cause i'm using this for adding effect in mouse move. So I used your jQuery code and it worked. Thanks :)
could you use the ":hover" CSS selector to add the affect on mouse hover over the LI tag?
Yes. But then I need to remove the transform after mouse leaving
2
$('ul.tabItem').css({"transform": "translateY(-12px)"});

or for hr tag

$('ul.tabItem hr').css({"transform": "translateY(-12px)"});

Comments

0

you can do this with just css no need to use jquery

like so:

<style>
.tabItem hr {
    transform: translateY(-12px)
}
</style>

Comments

0
$("ul.tabItem").addClass("yourClass");

With this, you can add a class to all of the elements that have the class tabItem already. In your class you can add all the styles that you need to provide, which is usually a better practice than just assigning it to the element itself.

If you still want to assign the styles directly, take a look at the css method in jQuery.

1 Comment

I understand this... however the answer does not seem too bad regarding the question itself How can I add css to all <ul>tags with .tabItem class at the whole page using jQuery?

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.