I've tried to create a code snippet for an example of a selector I was trying to use and it's not working. Can someone eyeball it and tell me what I have wrong?
var dateDiv = null;
var expenseDate = "06/22/2016";
$(":data(date)").each(function() {
var element = $(this);
element.css("backgroundColor", element.data("color"));
});
.expense-item {
margin-left: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="contentWrapper">
<div data-date="06/22/2016" data-color="red">
06/22/2016
<div class="expense-body">
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (3)
</p>
</div>
</div>
</div>
<div data-expense-date="06/23/2016" data-color="blue">
<div class="expense-body">
06/23/2016
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
</div>
</div>
<div data-expense-date="06/24/2016" data-color="yellow">
<div class="expense-body">
06/24/2016
<div class="expense-item">
<p>
This is an expense (1)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (2)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (3)
</p>
</div>
<div class="expense-item">
<p>
This is an expense (4)
</p>
</div>
</div>
</div>
</div>
My code was inspired by this example: http://api.jqueryui.com/data-selector/
You want an attribute selector:
Fiddle: https://jsfiddle.net/j3cmo4ow/5/
If you want the
:data
pseudo selector to work, you need to include jQuery UI.