I'm having a bit of trouble figuring out why my code is not working. In the below example html I have three nav-tab elements. I'm trying to target the second element (Products tab) in order to make the following changes:
- Add a red border to the div.nav-tab element on hover (Products tab only)
- Change the text of "Products" to "Merchandise"
I know this can easily be done by changing the CSS and HTML but i'm trying to do this using only jQuery.
In my script I am trying to create a jQuery variable and then use the .hover and .find methods on it, but I always get the error ".find -or- .hover is not a function".
Additionally, I may want to add more tabs to the beginning of the navbar at a later time, so i don't want to use something like a class selector and target the second element, because when i add more tabs later, this will end up not targeting the Products tab. e.g...
$( ".nav-tab" )[1].hover...
I'm sure there's an easy way to do this. Could someone please give some advice as to how best to make the changes with just using jQuery? Thanks in advance!
I've created an example fiddle
<nav>
<div class="nav-tab">
<a class="nav-link" data-name="About">
<div class="item-menu">
<span>About</span>
</div>
</a>
</div>
<div class="nav-tab">
<a class="nav-link" data-name="Products">
<div class="item-menu">
<span>Products</span>
</div>
</a>
</div>
<div class="nav-tab">
<a class="nav-link" data-name="Contact">
<div class="item-menu">
<span>Contact</span>
</div>
</a>
</div>
</nav>
<script>
$( document ).ready(function() {
let products = $( ".nav-tab" ).has( '[data-name="Products"]' ).get( 0 );
products.hover(
function() {$(this).addClass("red-border")},
function() {$(this).removeClass("red-border")}
);
products.find('span').innerText="Merchandise";
});
</script>