So, I’m making this site on WordPress, and I want in a card carousel for each item that has been selected the title to be different color, but I’m new to this and I don’t quite catch up with it.
So my question is can I make an array of className, because all of the items in carousel have the same class, but when the one item is selected, the all of the other items should be white or the color that it is i got this snippet here, this is jQuery for connected items and templates, you’ll figure it out. I tried to assign "id" onclick but its not working for me here is the code that is working for connected elements.
I was adding array function and tried to change foreach but I’m still new in js and jQuery so I just made a mess.
jQuery(function($) {
$('[data-showme]').on('click', function() {
var showme = $(this).attr('data-showme')
$('.all-content').hide()
$('#' + showme).show()
})
$("#slide-selected-1").on('click', function() {
$(".all-content").hide()
$("#smartedgemain").show()
})
})
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
3
Answers
When working with classes in HTML to JavaScript, it’s an array of classes.
So, to potentially implement what you’re looking for, something along the lines of:
`
Explanation:
1). Store the array of class names in the elements variable.
2). iterate over the array via for-loop.
3). attach event handler to each index and then you could have each index onClick call a function to manipulate each element.
Hope this helps.
Since you are looking to toggle a style on click, I have two solutions.
One uses the :focus pseudo class so it styles the focused matching element. The other one toggles a class that changes the color. First, it removes the class from anything already clicked, then it adds it to the currently clicked element.
If you only need to select the first/clicked card and highlight it and the rest will be in default color, this is the solution I’ve made…
In Javascript,
In JQuery: