I have a list of elements with Custom Data Attributes. I need to be able to add a class to another element on click when the data attribute of a clicked element matches the data attribute of that element.
HTML
<div data-my-element="value1">click 1</div>
<div data-my-element="another">click 2</div>
<div data-my-element="oneMore">click 3</div>
<section data-my-element="value1"></section>
<section data-my-element="another"></section>
<section data-my-element="oneMore"></section>
JS
$('div').click(function() {
var myEm = $(this).val('data-my-element');
$('section[data-my-element = '+myEm+']').addClass('clicked');
});
I think I'm doing something wrong.
Change:
To:
And if any of the data is being inserted or changed dynamically, you may consider using:
Try this:
You are also missing:
);
after}
at the end of your codeJSFiddle Demo
Change -
to this -
or this