If my markup looks like this:
<div data-test="{ "value" : "bar", "_id" : 1234, "name" : "john", "age" : 25 }">...</div>
<div data-test="{ "value" : "foo", "_id" : 1235, "name" : "paul", "age" : 26 }">...</div>
<div data-test="{ "value" : "drummer", "_id" : 1236, "name" : "ringo", "age" : 22 }">...</div>
How would I select a particular element using JQuery if I only had the key 'bar' or 'foo'?
I could pull out the whole object for each row and iterate through it looking for a match but I'd rather not if there is a more efficient method.
How can I cleanly select based on the property of an object?
$('#myDiv').data('value')returnsbarorfooso JQuery is able to operate on the object, no?JSON.parse()and match thevalueelement more cleanly. The difference is that you need to use double quotes rather than single quotes around the strings.