I have an style properties string like below
var style='max-width: 125px; height: auto; object-fit: cover; width: 125px; margin-left: auto; margin-right: auto;'
properties can be any. I want to check if max-width includes, and if it includes it will be deleted. But I couldn't resolve problem
var imgmaxwidth = style;
if (imgmaxwidth.includes("max-width")) {
self.$dialog.find('.note-image-attributes-style-maxwidth').val(value of string max-width);
}
I want to take this max-width value and then delete this property from string.
In final string will be
height: auto; object-fit: cover; width: 125px; margin-left: auto; margin-right: auto;
Thanks in advance
Try the following:
var style = 'max-width: 125px; height: auto; object-fit: cover; width: 125px; margin-left: auto; margin-right: auto;'
var styleArr = style.split(';')
var res = styleArr.filter(function (value) {
return !value.includes('max-width');
});
style = res.join(';');
console.log(style);
.as-console-wrapper { max-height: 100% !important; top: 0; }
var style = $('p').attr('style');
var parts = style.split(';');
var newstyle=[];
for (i=0; i < parts.length; i++)
{
if (parts[i].split(':')[0].toLowerCase().trim() != 'max-width')
{
newstyle.push(parts[i]);
}
}
$('p').attr( 'style', newstyle.join(';'));
I corrected for cases that would be missed if leading or trailing white space, or case issues. (prompted with comments by @Useless Code)