From a group of span elements, I want to get the values of the first occurrence of a span that is not empty.
I have this HTML
<span class="map_multi_latitude"></span>
<span class="map_multi_longitude"></span>
<span class="map_multi_latitude">30.201998</span>
<span class="map_multi_longitude">120.990876</span>
I have this Jquery before but I want to make it work such that "the first span whose text is not empty":
initialLat = $('span.map_multi_latitude:first').text();
initialLng = $('span.map_multi_longitude:first').text();
thanks in advance!
From a group of span elements, I want to get the values of the first occurrence of a span that is not empty.
I have this HTML
<span class="map_multi_latitude"></span>
<span class="map_multi_longitude"></span>
<span class="map_multi_latitude">30.201998</span>
<span class="map_multi_longitude">120.990876</span>
I have this Jquery before but I want to make it work such that "the first span whose text is not empty":
initialLat = $('span.map_multi_latitude:first').text();
initialLng = $('span.map_multi_longitude:first').text();
thanks in advance!
Share Improve this question edited Jun 15, 2011 at 3:37 BalusC 1.1m376 gold badges3.6k silver badges3.6k bronze badges asked Jun 15, 2011 at 3:26 yretutayretuta 8,09118 gold badges84 silver badges153 bronze badges3 Answers
Reset to default 16This will work:
$('span:not(:empty):first').text()
or these:
$('span:not(:empty):eq(0)').text()
$('span:not(:empty)').eq(0).text()
$('span:not(:empty)').slice(0,1).text()
$('span:not(:empty)').first().text()
initialLat = $('span.map_multi_latitude:not(:empty):first').text();
initialLng = $('span.map_multi_longitude:not(:empty):first').text();
var firstMapMultiLatitude;
$('.map_multi_latitude').each(function (index, element) {
if ($(element).val() != '') {
firstMapMultiLatitude = $(element).val();
alert($(element).val());
return;
}
});