Results 1 to 10 of 10

Thread: Using jQuery to watch for changes

  1. #1
    Join Date
    Mar 2006

    Question Using jQuery to watch for changes

    Good day, all,

    I am trying to understand a fundamental feature of jQuery but, so far, cannot find any specific examples despite my Google-fu.

    My problem:

    There exists a set of scripts and Ajax calls that modify and change some elements on a page I need to update. I cannot touch or change these scripts; they are undocumented and the individual who built them is unavailable. All I am free to do is build scripts that watch the elements on the Web page and, when they change, do something.

    It's the "when they change" part that mystifies me.

    I'm trying to watch a div (let's call it id="dr_someUniqueDiv") and, when it becomes visible do "x" and when it becomes invisible do "y". My problem is that I do not know how to attach such a monitoring script to div#dr_someUniqueDiv or trigger my subsequent actions when it flips between display:block and display:none.

    I am stuck using jQuery 1.3.2.

    I know that .bind() or .live() is probably what I need, but both require knowing what event handler to use. I've gone through the API documentation and cannot find an event handler that pays attention to the properties of an object and fires "true" when they change. My plan is to watch for the visibility condition on div#dr_someUniqueDiv and, when it changes, perform some evaluations and then fire the changes I need.

    Can someone provide me with a general approach?

    Here is the pseudo-code of what I am attempting:

    <div id="dr_someUniqueDiv">
    	Hello, world.
    <script type="text/javascript">
    			if ($(this).isVisible()) {
    				// Do something

  2. #2
    Join Date
    Nov 2010
    It's possible, but tricky and jQuery won't help you much. HTML elements don't have change events, so you can:
    Listen for a DOMAttrModified event but Chrome, safari and lesser IE don't support it
    Listen for a propertychange event but that's only IE
    Set a mutation observer but that has its own cross-browser dramas and is fairly new

    and then of course you have to make sure that the element exists at the time you're setting any of those things

    is there some user action that shows/hides the div? the way I've gotten around it in the past is just to put a listener on the button that hides the div, and do a check there to see if it is shown or hidden.

  3. #3
    Join Date
    Mar 2006
    Hmmm... Frustrating.

    There's no monitoring function in jQuery, then? Okay ... I'm going to have to see what I can do about getting a workaround.

    If anyone else can think of a solution, please post it; you would really make my day!


  4. #4
    Join Date
    Mar 2013
    Can you explain after what action your div gets hidden/visible? For example, if you click on this element, and it hides, you can use:

    $(element).on("click", function(){
    //if visible, do something

    Or search your scripts for any jQuery Code that hide elements, for example, search for the keywords "css, hide, show, fadeOut, fadeIn, attr, etc.", you should find the correct function this way and insert a callback.

    Another thing would be deleting parts of the script, until you are left with the function that hides your element. It's an annoying thing to do, but sometimes it helped me out finding certain functions in scripts.

    Hope this helps!

  5. #5
    Join Date
    Mar 2006
    You have been very helpful, xatm; I appreciate it!

    The issue is this: we had someone who is no longer available create a very cool set of code and get it approved for wide use. But it was minified and undocumented. Since it is a major library we can't just go in and modify it. We'd have to clone it and reverse-engineer the code to find the on-click instructions and, right now, that's not feasible.

    In the future, we're going to re-create the whole thing (documented this time) and not have this problem. For now, I was just hoping I could create an entirely external script that could monitor the page for changes and go from there.

    Thank you, though, for your answers; I do appreciate it!


  6. #6
    Join Date
    Mar 2013
    Not a problem!

    So either the user has to actively do something, in order to execute the function that hides the element in your script, or it somewhat gets executed automatically without any user action, right?

    If the user has to do something, like i said for example click on something, to execute the function from your scripts that hide the element, you can listen for those clicks in the code of my previous posts, and then check if xxx is hidden or not, independently from your script functions.

    If your function gets executed automatically, for example after the dom is loaded, you can insert a script at the bottom that checks if the element is visible.

    If your function gets executed automatically without any user input, after a certain time, you can make a loop that checks if the element is visible every xxx seconds, its not the most elegant way to do that, but maybe it suffices for your cause!

  7. #7
    Join Date
    Mar 2006
    It's a strange scenario.

    We have this script which we cannot touch or modify that intercepts clicks and interprets them before performing one of several actions on the page. It's complicated enough that attaching new clicks to the existing user interface leads to confusion depending on when things get executed. And, as mentioned before, I cannot chain my commands to the existing links since I am not allowed to edit those scripts.

    As a result, we have to watch for the resulting actions on the page which arise from a user's actions.

    What we eventually did was insert a click-monitoring piece of code that would supersede some of the ensuing activity. That seems to have worked for the time being. It creates spaghetti-code, layering functions on top of functions, but it was all we were left with in the end.

    Again, thank you very much for your time and assistance; I truly appreciate it.


  8. #8
    Join Date
    Mar 2009
    It might be wholly inappropriate for your purposes, but this is a simplified version of something I did once for a similar situation that I dug up from memory. I just threw this together so it may have syntax or logical errors in it. In any event hopefully it can be of some use to you.

    //args (1) array of element ids you want to monitor (2) CSS property to be checked, and
    //(3) function to which the report is sent to
    function reporter(idArray, CSSproperty, reportTo){
        var x = {};
        x.id =[];
        x.ref = [];
        x.report = reportTo;
        x.prop = CSSproperty;
        var i, len;
        //get actual refs to speed things along
        x.id = idArray;
        len = x.id.length;
        for(i = 0; i < len; i++){
    	x.ref[i] = document.getElementById(x.id[i]);
        //scan and record the settings of all the elements you are interested in
        //record values in an object that is returned from the function
        x.scan = function(){
    	var i, summary, len = x.ref.length;
    	summary = {};
    	for(i = 0; i < len; i++){ 
    	    summary[x.id[i]] = x.ref[i]['style'][x.prop];
        //try to insure that the function that does something to the page has 
        //completed before doing the scan. 25ms was an arbitrary choice
        x.run = function(){
    	setTimeout(x.scan, 25);
        //attach to window.onclick, if it exists
        var fn = window.onclick;
        if(typeof(fn) == 'function'){
    	window.onclick = function(a,b,c,d,e){ //hope it doesn't need 6 or more args
    	window.onclick = function(){
        return x;
    /*==============that's the reporter.  To use an example, lets say we have have 3 elements
    and we want to monitor the status of each one in regards to its 'display' CSS property
    whenever the user clicks on the screen.  The id's are El1, El2, and El3.  We also need a
    function as the 3rd arg to send it to--it could log it or send it off via AJAX or socket,
    but we'll just do this:
    function report(obj){
       var i;
       for(i in obj){
        alert(i + ":  " + obj[i]);
    If --and make sure the reporter function doesn't run until the page is loaded and all (if any)
    elements have been created dynamically-- we do this, you should be provided with a snapshot
    every time the screen is clicked.
    var whatever = reporter(['El1','El2','El3'],'display',report);

  9. #9
    Join Date
    Mar 2006

    Let me just say "Thank You"!

    I'll go over this in my spare time and suss out its contours and functions. I truly appreciate this! Thank you very much!


  10. #10
    Join Date
    Mar 2009

    One thing about the code as well that you should know if you don't already is that the 'style' object of an element usually reflects only the "inline" style of the element. The snapshots won't necessarily reflect any real values unless and until those qualities have been set by inline HTML CSS or until the js changes them.

    If you want to have a complete picture from the beginning you might use the jQuery.css thing to get the current setting for each element. That will only require minor modification of the code.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11572 seconds
  • Memory Usage 2,954KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (10)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates