JSFiddle - React, Tailwind, and code Playground

by mblase75

HTML

<div id="MyDiv"></div>
<div id="SecondaryDiv"></div>

JavaScript

// Set the HTML5 data key 'name' to value 'MyName'
$("#MyDiv").data("name", "MyName");

// Return all divs with the data key 'name' matching 'MyName'
//     This works without a hitch
$d1 = $("div").filter(function () {
    return $(this).data("name") === "MyName";
});
console.log($d1);

// This breaks because I try to add '.toLowerCase()' to the end of the .data() function.
// The error returned is:
//     TypeError: $(...).data(...) is undefined
$d2 = $("div").filter(function () {
    if ($(this).data("name")) {
        return $(this).data("name").toLowerCase() === "myname";
    } else {
        return false;
    };
});

console.log($d2);