I need to change data attribute "aria-selected" by oncklick.
But this script does not work. Could you help me, please?
<a href="#" aria-selected="true" resource="">SHOW/HIDE</a>
And here is my code:
<script>
$(document).ready(function($){
$("a").attr("aria-selected","false");
$(" ul li a").addClass("accordion");
$('.accordion').click(function() {
if ($(this).attr('aria-selected')) {
$(this).attr("aria-selected","true");
}
else {
$(this).attr("aria-selected", "false");
}
});
});
</script>
The
aria-selected
is a string... Not a boolean.So you have to compare it with a string.
is supposed to be
You are explicitly setting
aria-selected
to false on page load. When the element withaccordion
class is clicked, you seem to toggle the attribute value. But in your case it will always be set to false, cause of your existingif
condition.You can modify the code to make it a bit cleaner