Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi Friends,
Below is my HTML code
<div class="k-state-border-up">
   <div class="k-tooltip-content">
      <ul class="toolTipList"><ul>
      <hr></hr>
      <ul class="toolTipList">
         <span>Bing Ads</span>
         <li><a>Campaigns</a></li>
         <li><a>Test</a></li>
      <ul>
   </div>
</div>


I want to find the text Campaigns form second ul list, how can I do using css selector?

I tried following, but did not succeed
.k-state-border-up > .k-tooltip-content > .toolTipList:nth-child(2) > li:nth-child(1)

Can anyone help?

thanks
Posted
Updated 1-Jul-14 1:49am
v2

Just to support what ProgramFOX has said, below is the Demo...

Demo


[Issue] Getting the List Item "Campaigns" by Class Selectors[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Jul-14 12:21pm    
5ed.
—SA
Thanks Sergey. :)
It does not work because your HTML code is invalid:

  • You put a ul, a hr and a span element directly inside an ul element. This is not allowed, an ul tag can only contain li tags (and in li tags, it is allowed to put other elements such as hr and ul)
  • You didn't close the ul tag

If you fix these problems, your HTML code will look like this:
HTML
<div class="k-state-border-up">
   <div class="k-tooltip-content">
      <!-- remove the <ul class="toolTipList"><ul> line here -->
      <span>Bing Ads</span>   <!-- put the span outside the list -->
      <hr></hr>
      <ul class="toolTipList">
         <li><a>Campaigns</a></li>
         <li><a>Test</a></li>
      </ul> <!-- close the tag -->
   </div>
</div>

Then, use this CSS:
.k-state-border-up > .k-tooltip-content > .toolTipList > li:nth-child(1)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Jul-14 12:21pm    
Well spotted, a 5.
—SA
Thomas Daniels 1-Jul-14 12:39pm    
Thank you!
Besides the invalid HTML cited by @ProgramFOX, it looks like you have misinterpreted the meaning of nth-child().

As described in the jQuery documentation (http://api.jquery.com/nth-child-selector/[^]), the :nth-child() selector counts all children of each parent, regardless of any selector attached to the pseudo-class.

So .toolTipList:nth-child(2) refers to any element having class toolTipList that is also the second child of its parent. It does not necessarily refer to the second toolTipList element.

If you meant for your HTML to be this:
HTML
<div class="k-tooltip-content">
   <ul class="toolTipList"> ... </ul>
   <hr>
   <ul class="toolTipList">
      <li><span>Bing Ads</span></li>
      <li><a>Campaigns</a></li>
      <li><a>Test</a></li>
   </ul>
</div>

then there is no element that has the toolTipList class that is also a 2nd child (the <hr> element is the 2nd child of its parent, but the toolTipList elements are 1st and 3rd children).
 
Share this answer
 
v3
Comments
dhage.prashant01 4-Jul-14 0:58am    
I was bit confused on this
If I say .k-tooltip-content > .toolTipList:nth-child(2), why it considers the hr as second child?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900