JSFiddle - React, Tailwind, and code Playground

HTML

<div id="test1">1</div>
<div id="test2" data-test="test">2</div>
<div id="test3">3</div>

<br>

<div id="output"></div>

JavaScript

$("#test1").data("test", "test1");

var dataLength = $("div").filter(function() {
   return $(this).data("test") != undefined; 
}).length;
    
$("#output").text("There are " + dataLength  + " divs with data");