views:

109

answers:

2

Hi All, I want to know how to get access of this [span class="myclass"] in below html structure..

        <ul>
    <li class="first">
      <span class="myclass"></span>
      <div class="xx">

        <span></span>
        ........
          </div>
   <li >
      <span class="myclass"></span>
      <div class="xx">

        <span></span>
         ........
      </div>
     </ul>

Here I need to write one function in [span class="myclass"], but i cant do it using $(".myclass") [I have few issues] I just want to directly access the span itself.How to do this..?

EDIT:the sln by phoenix is working fine..but lets say(just for my knowledge) the structure is

  <li >
      <span class="myclass"></span>
      <div class="xx">
        <li>
        <span></span>
       </li>
         ........
      </div>
   </ul>

so why the span inside 2 nd li(which is under div) is not getting the ref, is it bcoz they are not in the same level..if I need to access them do I need to do some thing like

enter code here

$("li").next(".xx").find(li span:first-child )..or something else is there? Thanks.

+4  A: 
$("li span.myclass")

EDIT: Okay then maybe with

$("li span:first") //EDIT: Don't do that. See below.

apparently :first stops after the first hit. So :first-child is the way to go.

which will select the first span in every li-element. But this can be tricky in case you have other li-elements with spans inside...

EDIT: If you can't use the class you already have, maybe assigning an additional class helps?

<span class="myclass newClass"></span>
...
var spans = $("li span.newClass");

EDIT: As phoenix pointed out

$("li span:first-child")

returns a list with all span elements that are the first child of a li-element. I don't know if jQuery treats textnodes as child nodes. So if you have a space between <li> and <span>, this might be counted as the first-child. So check if you have any whitespace between your elements beside line breaks.

moxn
I cant use myclass
Wondering
also the function should get call from span(one at a time..its like a expandable panel
Wondering
What function should get what? You want to call a function for each span? use $.each($("li span:first"), function(i, val){...});
moxn
Or a better question: Why can't you use the class attribute?
moxn
+1  A: 

If span is the first child then you can use

first-child

selector

$("li span:first-child");
rahul
but then what about 2nd span under 2 nd li..i need to call the function from there also?will it work?
Wondering
edited my code. This will work for you.
rahul
li.first will not work for 2nd span under 2nd li(it dont have a class)
Wondering
I have already edited my code for that.
rahul
yes,and its working like a magic....Thanks for all ur help.
Wondering
one doubt..i have edited the qs, can u pls take look
Wondering
@Wondering: I have updated my post
moxn
@moxn: What I am asking is $("li span:first-child"); is getting ref of <li > <span class="myclass"></span>but why not <li > <span class="myclass"></span> <div class="xx"> <li> <span></span> </lispan inside this div, this also first child of li...
Wondering
@Wondering: I know. This is what I described in the last edit of my post.
moxn