views:

36

answers:

3

I want choose all the li in my html that has the <span>Google Map</span> in it and apply and get the value in href with jquery. I tried going through jquey docs but coulding figure it out. Is this possible?

<li><span>Google Maps Staic Link:</span> 
<a target="_blank" href="**Value To Get**" rel="nofollow">Map</a></li>

The actual HTML will be something like this.

<li><span>Name</span>Newyork:</li>
<li><span>Added:</span>23rd April</li>
<li><span>Google Map Link:</span> 
<a target="_blank" href="**Value To Get**" rel="nofollow">Map</a>
</li>

From this I only want the one with Google Map Link. The link will change but the span <span>Google Map Link:</span> will not change.

+1  A: 
$('span:contains("Google Map")').next('a').attr('href');

that will give you an jQuery array object of all the hrefs

Moin Zaman
Worked exactly as I wanted. Thanks a million.
esafwan
This would only get the first occurence, not an array.
jAndy
in my case... there is no array. So ok for me.
esafwan
+1  A: 
$(function(){
   var href = $('li:has(span:contains(Google Map))').find('a').attr('href');
});​

You would need to invoke .each() if you expect more than one occurence.

Anyway, selectors like this look so wierd and odd, I'd suggest to use .filter() anyway.

jAndy
+1! First one worked for me, but when as u said more than one occurrence is there this is the best solution. Maybe handy for future use.
esafwan
A: 
​$('span:contains("Google Map")').each(function(index){
    alert($(this).siblings(0).attr('href')); //Do whatever with href attr...
});​​​​​​​​​
mamoo