Promo
Promo

Reputation: 737

Need help to target element with jquery

I need some help to select an element in jquery...

With the following code

    <ul>
        <li>
            <div>
                <div>
                    <a href="#" id="target">link</a>
                </div>
                <span>Hello</span>
            </div>
        </li>
    </ul>

I need to target the <span> tag but I only know the id of <a> tag.

Someone can help me ?

Thanks

Upvotes: 0

Views: 142

Answers (4)

Ankit Agrawal
Ankit Agrawal

Reputation: 6124

<script>
    jQuery(document).ready(function(){
    a=jQuery("#target").parents('li').find('span').html();
    alert(a);
    });
    </script>

you can target span by this code

Upvotes: 0

siliconrockstar
siliconrockstar

Reputation: 3664

I don't know if closest() will work because it only searches up the DOM tree, through ancestors. Try

$('#target').parent().siblings('span');

If you're not sure just how far back up the DOM tree the span is, you can test the siblings of each parent of #target. I could be wrong on all this though lol

Upvotes: 0

jfriend00
jfriend00

Reputation: 708206

You can go up to the parent and then get the next element which will be the <span>:

$("#target").parent().next()

Upvotes: 1

Jamiec
Jamiec

Reputation: 136239

$('#target').parent().next('span');

Upvotes: 0

Related Questions