Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 666637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:50:25+00:00 2026-05-13T23:50:25+00:00

Is there any way that I can call a JavaScript function via css? For

  • 0

Is there any way that I can call a JavaScript function via css?

For example, from this :hover style:

.first-nav li a:hover,
.first-nav li.hover a {
    margin:-3px 0 -1px;
    height:30px;
    position:relative;
    background:url(../images/nav-hover.jpg) no-repeat;
}

The JavaScript function I want to call is on an anchor :hover.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:50:25+00:00Added an answer on May 13, 2026 at 11:50 pm

    No, you can’t trigger JavaScript from CSS directly.

    What you can do is use CSS selectors to find the elements you want to watch in this way, and then watch for mouse events. The standard events are mouseover and mouseout, but they can be a bit tricky to work with because they bubble (you get mouseout, for instance, whenever the mouse leaves any descendant element). With appropriate logic, though, they’re not to bad to work with, and in fact if you have lots of these, you probably want to use mouseover and mouseout rather than the alternative below because you can set them on just a parent container and then work out which descendant element is involved, which can be simpler in some cases (and more complicated in others).

    IE provides mouseenter and mouseleave which are much easier to work with because they don’t bubble, but (of course) IE-specific. These are so handy that frameworks are starting to support them even in browsers that don’t; Prototype and jQuery provide them, for instance, and I wouldn’t be too surprised if some other frameworks do as well. jQuery also provides the handy hover function, which would be very close to what you want:

    // jQuery
    $(".first-nav li a").hover(
        function(event) {
            // The mouse has entered the element, can reference the element via 'this'
        },
        function (event) {
            // The mouse has left the element, can reference the element via 'this'
        }
     );
    

    …which is really just a shortcut for setting up mouseenter and mouseleave handlers, but still, wonderfully concise.

    In Prototype it’s quite similar:

    // Prototype
    $$(".first-nav li a")
        .invoke("observe", "mouseenter", function(event) {
            // The mouse has entered the element, can reference the element via 'this'
        })
        .invoke("observe", "mouseleave", function(event) {
            // The mouse has left the element, can reference the element via 'this'
        });
    

    (OT: In both cases, I’ve used anonymous inline function expressions just to avoid giving the impression you have to use named functions. I always recommend using named functions in production code, though.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 478k
  • Answers 478k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use a synchronization variable. In main: pthread_mutex_lock(&my_cond_m); while (!flag) {… May 16, 2026 at 5:28 am
  • Editorial Team
    Editorial Team added an answer To expand on the comment from Yardena, there is an… May 16, 2026 at 5:28 am
  • Editorial Team
    Editorial Team added an answer If the code that calls the method that throws the… May 16, 2026 at 5:28 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.