I am looking for modifying the class name of a div according to the name of another div.
I cannot make it work with JQuery.
Here is the html structure :
<div class="div-overlay">
<a class="image" href="https://www.example.net/page-events/" target="_blank"></a>
<div class="div-container">
<ul class="post-categories">
<li>
<a href="https://www.example.net/category/events/" class="category-item">Events</a>
</li>
</ul>
</div>
<div class="div-overlay">
<a class="image" href="https://www.example.net/page-conferences/" target="_blank"></a>
<div class="div-container">
<ul class="post-categories">
<li>
<a href="https://www.example.net/category/conferences/" class="category-item">Conferences</a>
</li>
</ul>
</div>
My goal is to modify the :
class="div-overlay"
According to the text value in :
class="category-item"
My attempt is not successful :
$(document).ready(function(){
var category = $(".category-item").html();
var newEvents = $(".div-overlay").attr('newclass','events');
var newConferences = $(".div-overlay").attr('newclass','conferences');
if (category = "Events"){
newEvents;
} else if (category = "Conferences") {
newConferences;
};
});
How can I make my script work ?
Details : working on WordPress with elementor.
2
Answers
You should use
addClass
methdYou can try this code :