You really should find a better way of linking the anchor to the span - i.e. use IDs or data-
attributes.
But since you asked for searching for the nearest span
going upwards, here you go:
$(".domain").click(function(){
var $anchor = $(this);
$("div#list_lm_domain_urls_dialog").dialog('open');
var domain = $anchor.text();
var network_ip;
$anchor.parents().each(function(){
var $spans = $(this).prevAll().find('span.network_ip');
if ($spans.length>0) {
network_ip = $spans.last().text()
return false;
}
});
alert(network_ip);
refresh_lm_domain_links(domain,0,100);
return false;
});
This takes all the parents of the anchor and loops from the nearest parent upwards. For each parent it checks previous siblings using .prevAll()
and looks for the span. Finally, it takes the last from the found spans - i.e. the "bottom" span if there were more than one.
Working demo here.