One

JQuery data is selected from several names

I have such a setup where I need to get one of data-targets:

<div data-target="one">One</div>
<div data-target="two">Two</div>
<div data-target="three">Three</div>

How can I write a selector that allows me to target data-target="one"

+4
source share
2 answers

To do this, use the base Attribute Selector . Thus, you can target this way:

$('[data-target="one"]');

And getting the data item can be done with:

$('[data-target="one"]').data("target");

Make sure you are using the latest version of jQuery. If not, you can also use pure JavaScript :

document.querySelectorAll('[data-target="one"]');
+3
source

CSS , div[data-target="one"]

function jChoice(val) {
    return $('[data-target="' + val + '"]');
}
0

Source: https://habr.com/ru/post/1622347/


All Articles