[RESOLVED]How select element with data attributes after dynamically set them?

<div data-test="1"></div>
        var $div = $('[data-test="1"]');
        console.log($('[data-test="1"]')); //found

        $div.data('test', "2");
        console.log($('[data-test="1"]')); //found
        console.log($('[data-test="2"]')); //not found
        console.log($('[data-test=2]')); //not found

as my code, after I set data-test by .data() method, I still have to query the element by original value, not current value.

What’s wrong here?

workaround is use $div.attr("data-test", 2),

even $div.prop("data-test", 2) won’t work

Leave a Reply