I'm aware that there's a way to check if an attribute exists by doing something like:
if ($("div").data('attr')) {
// true
}
However, is there a way to to find attributes that are similar and check if they exist?
For example:
.listing div which has data-dept="sales".card div which has data-dept="sales-us"If .card has a data-dept attribute that is similar to that of .listing, then I'm trying to append .card to .listing. If it doesn't match, then append .card to .listing[data-dept="other"].
var html = "<div class='card' data-dept='sales-us'>Test</div>";
if ($(".card").data("sales")) {
$(".listing[data-dept='sales']").append(html);
} else {
$(".listing[data-dept='other']").append(html);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Examples of what similar means in the context:
.card is [data-dept="sales-us"] and .listing is [data-dept="sales"], these are similar as both contain "sales"..card is [data-dept="business"] and .listing is [data-dept="business-development"], these are similar as both contain "business".