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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:23:46+00:00 2026-06-11T18:23:46+00:00

I think this might be a passing the argument problem, but I’m still new

  • 0

I think this might be a passing the argument problem, but I’m still new to jQuery. I have two block elements that start out with a jQuery function re-styling one from the other. But when I use jQuery to replace the contents of those block elements with the same kind of code, the re-styling function no longer works.

This is the core of what I’m working with:

$(document).ready(function() {
    $('#baz').click(function() {
        $('#bar').replaceWith('<div id="bar">' + 'texttexttexttexttext' + '<span id="hl1" class="nohighlight">' + 'some other stuff to be highlighted' + '</span>' + 'texttexttexttext' + '</div>');
        $('#foo').replaceWith('<div id="foo"><span id="rollover1">' + 'Look at this!' + '</span></div>');
    });

    $('#rollover1').mouseenter(function() {
        $('#hl1').removeClass('nohighlight').addClass('highlight');
    });

    $('#rollover1').mouseleave(function() {
        $('#hl1').removeClass('highlight').addClass('nohighlight');
    });
});​

...

<div><a href="#" id="baz">change</a></div>
<div id="foo"><span id="rollover1">HEY!</span></div>
<div id="bar">blahblahblah<span id="hl1" class="nohighlight">blahblahblahblah</span>blahblahblah</div>​

See http://jsfiddle.net/xdP7Z/4/

  • 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-06-11T18:23:47+00:00Added an answer on June 11, 2026 at 6:23 pm

    You are dynamically changing the elements so you need to delegate

    $(document).ready(function() {
        $('#baz').click(function() {
            $('#bar').replaceWith('<div id="bar">' + 'texttexttexttexttext' + '<span id="hl1" class="nohighlight">' + 'some other stuff to be highlighted' + '</span>' + 'texttexttexttext' + '</div>');
            $('#foo').replaceWith('<div id="foo"><span id="rollover1">' + 'Look at this!' + '</span></div>');
        });
    
        $('body').on('mouseenter','#rollover1', function() {
            $('#hl1').removeClass('nohighlight').addClass('highlight');
        });
    
        $('body').on('mouseleave','#rollover1',function() {
            $('#hl1').removeClass('highlight').addClass('nohighlight');
        });
    });
    

    FIDDLE

    It’s best to delegate to the closest parent element existing in the dom, so you should replace body with a static parent element. By delegating you attach the event handler to a parent element – which then listens for the event to bubble up and handles the event.

    I made my example using .on() because i saw you selected jQuery 1.8 but if you are using an older library – here’s how you should choose which method to use

    $(selector).live(events, data, handler);                // jQuery 1.3+
    $(document).delegate(selector, events, data, handler);  // jQuery 1.4.3+
    $(document).on(events, selector, data, handler);        // jQuery 1.7+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this might be a problem with the theme I'm using ( Nitobe
I mostly develop using C#, but I think this question might be suitable for
I think my code might be written incorrectly and this is causing my problem.
I think this could be a very easy question for you. But I have
This is my first post and I have am concerned that some might think
This might be something very straight forward and I really think it should work
Firstly, This might seem like a long question. I don't think it is... The
You might think that this question is already answered. However, I couldn't find the
I really nead to ask you this one, many of you might think it's
This might be a stupid question, but I notice that in a good number

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.