Change Color of Icon When Hovering Over Adjacent Text With jQuery

I have seen this article but I want to change the color of the icon with JavaScript instead.

Trying to write a function that changes the color of the icon and the text when the icon or the text is hovered over.

I’m using the code provided in this answer to check to see if the element is hovered with jQuery:

function changeIconColor (hoverState) {
let contactText = document.getElementsByClassName('contact-text');
let contactIcon = document.getElementsByClassName('contact-icon');  
//if the text is hovered over, change the color of the icon to #e46e6e
if ($('#contact-text').is(":hover")) {
    contactIcon.css("color", "red");
};
if ($('#contact-icon').is(":hover")) {
    contactText.css("color", "red");
};
}

changeIconColor();
.outer-one {
  display: flex;
  flex-direction: row;
}

.outer-two {
  display: flex;
}

.phone-text, .contact-text {
  color: #213b56;
  text-decoration: none;
  font-weight: bold;
  font-family: 'Raleway';
  margin-top: 4px;
}

.contact-text {
  margin-top: 7px;
}

.contact-text:hover {
  color: #e46e6e;
}

.user-icon, .contact-icon {
  padding: 7px;
}
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>

   <div class="outer-one">
   <div class="outer-two">
   <i class="far fa-user user-icon"></i>
   <span class="phone-text" style="font-family: Raleway, sans-serif; font-size: 
                                1.2rem;">(314) 567-7000  | </span>
   <i class="far fa-envelope contact-icon" id="contact-icon"></i>
   <a class="contact-text" href="http://cfk2021.flywheelsites.com/" id="contact-text">CONTACT</a>
   </div>
  </div>

As you can see, I’m loading jQuery in the <head> of the html, so I’m not sure why I’m getting a script error. Or check out this jSFiddle for reference.

114 thoughts on “Change Color of Icon When Hovering Over Adjacent Text With jQuery”

  1. Pretty section of content. I just stumbled upon your site and in accession capital to assert that I get in fact enjoyed account your blog posts.
    Anyway I’ll be subscribing to your feeds and even I achievement you access consistently fast.

    Reply
  2. Link exchange is nothing else except it is only placing
    the other person’s blog link on your page at suitable
    place and other person will also do same in favor of you.

    Reply
  3. It is perfect time to make a few plans for the future and it’s time to be happy.

    I’ve read this publish and if I could I wish to counsel you
    few interesting issues or tips. Perhaps you can write subsequent articles referring to this article.
    I wish to read even more things approximately it!

    Reply
  4. I loved as much as you’ll receive carried out right here.
    The sketch is attractive, your authored material stylish.
    nonetheless, you command get bought an impatience over that you wish be delivering the
    following. unwell unquestionably come more formerly again since exactly the same
    nearly very often inside case you shield this increase.

    Reply
  5. Heya! I realize this is sort of off-topic however I had to ask.
    Does managing a well-established blog like yours require a
    large amount of work? I’m completely new to writing a blog but I do write in my diary on a daily basis.
    I’d like to start a blog so I will be able to share my
    experience and views online. Please let me know if you have any
    kind of ideas or tips for brand new aspiring blog owners.
    Thankyou! quest bars http://bitly.com/3jZgEA2 quest bars

    Reply
  6. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog
    that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite
    some time and was hoping maybe you would have some experience with
    something like this. Please let me know if you run into anything.

    I truly enjoy reading your blog and I look forward to your new updates.
    cheap flights http://1704milesapart.tumblr.com/ cheap flights

    Reply

Leave a Comment