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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:22:35+00:00 2026-05-31T04:22:35+00:00

How can I trigger an event when a div changes its height or any

  • 0

How can I trigger an event when a div changes its height or any css attribute?

I have a div with id = mainContent. I want jquery to automatically trigger an event when it changes its height. I did something like this:

$("#mainContent").change('height', function() {
    $("#separator").css('height', $("#mainContent").height());
});

I know its wrong.

Here’s my whole code (I pasted all of it because I can’t get into jsfiddle for some reason I don’t know):

$(document).ready(function() {
 $("#separator").css('height', $("body").height());
});

$(function() {
 $("#btnSample1").click(function() {
  $("#mainContent").css('height', '400px');
  $("#mainContent").css('width', '600px');
  $("#mainContent").css('background-color', '#F0F0F0');
 });

 $("#btnSample2").click(function() {
  $("#mainContent").css('height', '1600px');
  $("#mainContent").css('width', '700px');
  $("#mainContent").css('background-color', '#F0F0F0');     
 });

 $("#mainContent").change('height', function() {
  $("#separator").css('height', $("#mainContent").height());
 });
});
html, body {
 width: 100%;
 height: 100%;
 margin: 0;
 padding: 0;
}

#separator {
 border-right: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table style="width: 100%;">
 <tr>
  <td valign="top" style="width: 19%;"> 
   <table id="mainMenu">
    <tr><td><input id="btnSample1" type="button" value="Sample 1"  /></td></tr>
    <tr><td><input id="btnSample2" type="button" value="Sample 2"  /></td></tr>
   </table>
  </td>

  <td valign="top" style="width: 1%;" >
   <div id="separator"></div> 
  </td>

  <td valign="top" style="width: 80%;">
   <div id="mainContent"></div>
  </td>
 </tr>
</table>

I am trying to adjust the height of the div id=separator based on the height of mainContent whenever the height of mainContent changes.

PS: In this case I know I can use the button event to do this but I want the div to trigger the event when the height is changed.

  • 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-31T04:22:36+00:00Added an answer on May 31, 2026 at 4:22 am

    First, There is no such css-changes event out of the box, but you can create one by your own, as onchange is for :input elements only. not for css changes.

    There are two ways to track css changes.

    1. Examine the DOM element for css changes every x time(500 milliseconds in the example).
    2. Trigger an event when you change the element css.
    3. Use the DOMAttrModified mutation event. But it’s deprecated, so I’ll skip on it.

    First way:

    var $element = $("#elementId");
    var lastHeight = $("#elementId").css('height');
    function checkForChanges()
    {
        if ($element.css('height') != lastHeight)
        {
            alert('xxx');
            lastHeight = $element.css('height'); 
        }
    
        setTimeout(checkForChanges, 500);
    }
    

    Second way:

    $('#mainContent').bind('heightChange', function(){
            alert('xxx');
        });
    
    
    $("#btnSample1").click(function() {
        $("#mainContent").css('height', '400px');
        $("#mainContent").trigger('heightChange'); //<====
        ...
    });    
    

    If you control the css changes, the second option is a lot more elegant and efficient way of doing it.

    Documentations:

    • bind: Description: Attach a handler to an event for the elements.
    • trigger: Description: Execute all handlers and behaviors attached to the matched elements for the given event type.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I trigger a change event on a jQuery UI slider ? I
I want to trigger an event when my mouse enters a div, and a
I have a newbie question about jQuery. Why is it that I can trigger
Like how the click() can be used to trigger a click event on an
In SQL Server 2005 Integration Services, how can I automatically trigger a restoration of
I can't get didAccelerate to trigger. I have the delegate implemented in my header
I have this Trigger in Postgresql that I can't just get to work (does
A knockout datepicker data binder can be found here: jQuery UI datepicker change event
I have registered a trigger on window resize. I want to know how I
Possible Duplicate: How to show/hide a div on mouseover using jquery? I have a

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.