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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:19:58+00:00 2026-06-05T04:19:58+00:00

I have this structure for one of my pages: head script /script /head body

  • 0

I have this structure for one of my pages:

head  
    script  
   /script  
/head  
body  
    table1  
    table2  
/body  

here is the javascript:

<script type="text/javascript">
    $( function() {
      $('tr').click( function() {
         $(this).parents('table').find('tr').each( function( index, element ) {
              $(element).removeClass('selected');
         } );
         $(this).addClass('selected');
         $(this).removeClass('normal');
      } );
    } );
    $( function() {
      $('tr').hover( function() {
         $(this).addClass('highlight');
         $(this).removeClass('normal');
      } );
    } );
    $( function() {
        if($('tr').hasClass('selected')) {
        }
        else {
          $('tr').mouseout( function() {
             $(this).removeClass('highlight');
          } );        
        }
    } );
</script>

table1 is generated by loading the page, while table2 is generated by clicking on any row of table1, using Ajax.

upon click or hover, the script adds some classes to tr (table rows) so I can style them (change background color)

the script applies to table1 rows and adds classes, but does not apply to table2 rows!

any help would be 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-06-05T04:19:59+00:00Added an answer on June 5, 2026 at 4:19 am

    These events only apply to elements that exist when the page is loaded. You need to use “live” events for them to affect the AJAX-loaded table (table2).

    For this, you can use jQuery’s .on:

    $(function(){
        $(document).on('click', 'tr', function() {
             $(this).parents('table').find('tr').each( function( index, element ) {
                  $(element).removeClass('selected');
             } );
             $(this).addClass('selected');
             $(this).removeClass('normal');
        });
        $(document).on('hover', 'tr', function() {
             $(this).addClass('highlight');
             $(this).removeClass('normal');
        });
        $(document).on('mouseover', 'tr', function(){
            if(!$(this).hasClass('selected')){
                $(this).removeClass('highlight');
            }
        });
    });
    

    Note: Instead of document, you can use the highest parent of both tables (as long as it stays in the DOM).

    UPDATE: The if($('tr').hasClass('selected')) { only ran when the DOM loaded. Put that check inside the event.

    P.S. You can combine all those $( function() { together.

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

Sidebar

Related Questions

I have the following file file: <html> <head> <title></title> <script type=text/javascript src=/Prototype.js></script> <script type=text/javascript>
I have two arrays the first one has this structure Parameter Keys array array
The typical Python threadpool will have a structure like this one: def run(self): while
I have this structure on form, <input type=test value= id=username /> <span class=input-value>John Smith</span>
I have quite simple site structure with one mastepage and a bunch of pages.
Hi guys we have following structure in one of our project's pages: <asp:UpdatePanel ID=up1
I have pages with structure like this some_page?id=123 Sometimes records are deleted, but those
I have this structure: Activity A has a ViewPager. The pages of a ViewPager
I have this structure Customer - has many Orders - has many OrderItems I
I have this structure based multimap and a vector for this structure: typedef std::multimap<char,

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.