javascript - Get product ID from class name eg 'productID_123' -
i have added class list of product images eg 'productid_123', number end. need dynamic possible image may have more 1 class. have put far, think there can't figure out if:
$(".image").each(function(){ var classlist = $(this).attr('class').split(/\s+/); $.each( classlist, function(index, item){ if (item === 'productid_') { product_id = item.replace("productid_",""); fetchproductimages(product_id,this.width,this.height); } }); }); maybe can enforce integer returned?
i can't use data-* attribute because page written in xhtml strict , required validate.
you use regular expression:
$.each( classlist, function(index, item){ var matched = item.match( /^productid_(\d+)$/ ); if (matched) { var product_id = matched[1]; fetchproductimages(product_id,this.width,this.height); } });
Comments
Post a Comment