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

  • Home
  • SEARCH
  • 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 7181335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:32:09+00:00 2026-05-28T17:32:09+00:00

I have the jQuery function below which alters the CSS on hover of a

  • 0

I have the jQuery function below which alters the CSS on hover of a p element.

$("p").hover (
function() {$(this).addClass("choice"); } ,
function() { $(this).removeClass("choice");} );

The problem is that it will not function for the innerHTML of a div written with .html() or .append(), like so

$('#div').html('<p>content that needs to highlight on hover</p>');

Even if I try to insert the function itself to that innerHTML like so

$('#div').append('<script type="text/javscript">$("p").hover ( function() {$(this).addClass("choice"); } , function() { $(this).removeClass("choice");} );<'+'/script>');

it will still not function for the HTML content of that div.

What am I missing here? Thanks, any help would be much appreciated.

  • 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-28T17:32:10+00:00Added an answer on May 28, 2026 at 5:32 pm

    When you write $("p").hover, that does a one-time search of the current state of the document, finds all <p> tags and attached event handlers to them. It will not attach event handlers to <p> tags you add to the document in the future.

    If you want to have event handlers for future <p> tags, then you need to use .delegate() (for pre-jQuery 1.7) or .on() (for jQuery 1.7+).

    With jQuery 1.7+, it would work like this:

    $(document.body)
      .on("mouseover", "p", function() {})
      .on("mouseout", "p", function() {});
    

    You have to use mouseover and mouseout events because hover with two separate callback functions is not supported with .on() (at least it wasn’t the last time I checked).

    Ideally, you wouldn’t use document.body in this code, but would use some common parent element that is not dynamic (it’s already there and stays there) and is as close in the hierarchy to the <p> tags that you’re monitoring as possible. This keeps too many events from having to bubble all the way up to the document level and helps with performance.

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

Sidebar

Related Questions

Here's the story... I have a jQuery function that does something, this function is
I have the below jquery function $(function(){ $(select#rooms).change(function(){ $.getJSON(/admin/select.php,{id: $(this).val(), ajax: 'true'}, function(j){ var
I have some jquery that runs when a link is clicked (below). This allows
I have this javascript, jquery function, (below) It gets the text inside each table
i have below function which is being used to initialize a widget. jQuery.fn.initPortlet =
Currently I have the below jQuery which uses the colorbox plugin http://colorpowered.com/colorbox/ $(document).ready(function(){ $(.altbgcolor).hover(function()
I have a JS function below that generates an href which includes the JQ
I have a jQuery setInterval function named timerIncrement that times out (stops incrementing the
I have the following jQuery function: $(function(){ if ($(.menu li).hasClass(active)) { $(.active).css(margin-top, 6px); }
I have a jQuery post function that returns a response on success after the

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.