extract string2
by mtenschert
HTML
<div class="product">
<div class="best-price-label ng-binding" ng-bind-html="ShowTownMobileDiscountBadge | raw">
<span class="product__additionals__percentage">jetzt bis zu -27% bei online Reservierung</span>
<div class="promo-inner-container bordered"><strong class="ng-binding">Gratuit pour les enfants</strong><p class="ng-binding">Pour 1 adulte payant, 1 enfant gratuit jusqu'à 6 ans</p></div>
</div>
</div>
<div class="product">
<div class="best-price-label ng-binding" ng-bind-html="ShowTownMobileDiscountBadge | raw">
<span class="product__additionals__percentage">jetzt bis zu -35% bei online Reservierung</span>
<div class="promo-inner-container bordered"><strong class="ng-binding">Gratuit pour les enfants</strong><p class="ng-binding">Pour 1 adulte payant, 1 enfant gratuit jusqu'à 6 ans</p></div>
</div>
</div>
<div class="product not-in-town">
<div class="best-price-label ng-binding" ng-bind-html="ShowTownMobileDiscountBadge | raw">
<span class="product__additionals__percentage">jetzt bis zu -55% bei online Reservierung</span>
<div class="promo-inner-container bordered"><strong class="ng-binding">Gratuit pour les enfants</strong><p class="ng-binding">Pour 1 adulte payant, 1 enfant gratuit jusqu'à 6 ans</p></div>
</div>
</div>
JavaScript
$(document).ready(function() {
// prepare variable for all discounts
var discounts = [];
var product_pills_in_town = $('.product').not('.not-in-town');
// get html string vom page containing the discount like "jetzt bis zu -27% bei online Reservierung"
$(product_pills_in_town).find("span.product__additionals__percentage").each(function(index, variable) {
discounts.push(+$(this).text().match(/\d+/));
})
// order from smallest to largest number.
// the biggest discount is now at the end of the array
discounts.sort();
if (discounts.length > 0) {
var biggestDiscount = discounts[discounts.length - 1];
}
// only display my labels when a discount is found!!!
if (biggestDiscount) {
// found a discount, all is good
alert(biggestDiscount);
} else {
alert('No discount found!')
// error log!
}
});