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 7522629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:34:36+00:00 2026-05-30T02:34:36+00:00

I’m using a function to add images to a site. These go into a

  • 0

I’m using a function to add images to a site. These go into a DIV, using .html()
After that’s done I need those same images to show an effect on hover, but nothing happens when I hover them.
If I type the same code I use in the .html() function, same class, etc, hover works.

After lots of attempts, I narrowed my problem to this sample.
Turns out the problem is how the container DIV exposes the code added through .html() … and my hover functions can’t find the dynamically added tags.

Here’s my sample code:

<html>
<head>
<title>Problems</title>
<script type ="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<p id="TXToldone">This line was already here</p>
<div id="DIVnewone"></div>
<script>

function createnewone(){
   $txthtml="<p id=\"TXTnewone\">And this one got added dynamically</p>";
   $("#DIVnewone").html($txthtml);
};
$(document).ready(function() {
   createnewone();
}); 
$("*").on("click", function(){
   alert( $(this).attr("id") );
});

</script>
</body>
</html>

Notes:
– This is just a raw example of my real problem. This is not what I’m
actually trying to accomplish, but solving this one would make me
solve the real one 😉
– The first two functions are from the actual code … after document
is ready I load the pics and recursively add the html required to
each DIV …
– Third function is just so you guys can see my problem … It should
be showing an alert with the ID of the element you click on (and it
does for the 1st paragraph) … but check what happens when you click
the second one … you don’t see the ID for that paragraph but the ID
for the container DIV instead!
– (yes, it also gives alerts for the other parents, up to the document,
but hey, it’s just an example)
– If I manually add the paragraph to that same DIV, the “static”
paragraph shows its ID when clicked on, but the “dynamic” one still
shows the DIV ID.

Please, advise?
(also, I need the script to stay at the bottom of my code, so it can “see” all the pics)

EDIT so my point is clearer: I need a way to add content to a DIV so other functions see the new code.

How would you guys replace this line so the sample code works as intended?

$("#DIVnewone").html($txthtml);
  • 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-30T02:34:38+00:00Added an answer on May 30, 2026 at 2:34 am

    Have you tried appending the new element to the div rather than setting it via html(), though I believe .html() should work as well.

    function createnewone(){
       $txthtml="<p id=\"TXTnewone\">And this one got added dynamically</p>";
       $("#DIVnewone").append($txthtml);
    };
    

    then you can use the below ON function (1.7+) to do what you need it to do when you have the mouse over the selector (‘#TXTnewone’). Note that the below will need to go into the “$(document).ready(function() { ...here... }” function, this is to ensure the “body” tag has been loaded when assigning the on event to it.

    $(document).ready(function() {
        createnewone();
        $("body").on(
            {
                  mouseenter: function () { $(this).css("color","red"); }
                , mouseleave: function () { $(this).css("color","black"); }
            }
            , "#TXTnewone"
        );
    });
    

    Here is a jsFiddle of the above working.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
For some reason, after submitting a string like this Jack’s Spindle from a text

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.