Search code examples
htmlchildren

How can i get to the child of div?


How can i get to the second span and take from here:

a)content of this span

b)tip of this span

How can i distinguish these spans without using id or something?

<div class="yea">
    <span tip="red">apple</span>
    <span tip="orange">orange</span>
    <span tip="yellow">banana</span>
    <span tip="brown">pineapple</span>
</div>

Solution

  • You can use 'CSS3 :nth-of-type() Selector' to style your spans without using or having access to individual unique selectors.

    Example:

    span:nth-of-type(2) { // 2 grabs second instance
        background: red; // sample
    }
    

    You didn't add the jQuery or JavaScript tag to your question; but I believe this is what your looking for, for the other aspect of your question. .text();

    Example:

    $('span:nth-of-type(2)').text();