SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Binding Event to innerHTML

    Hi everyone,

    I'm trying to figure out a way to some how 'bind an event' to the innerHTML property. As an example, if I do something like:

    Code:
    <containerElement>.innerHTML='some HTML code';
    I would like somehow to be able to fire an event to accommodate the updated container content.

    I just spent today and yesterday teaching myself prototypes and literals and all that cool stuff; from what I was able to pick up, I came up with the following unfunctioning (go figure ) script:

    Code:
    <script language="javascript" type="text/javascript" src="prototype.js"></script>
    <script language="javascript" type="text/javascript">
    
    Object.prototype.initialize = function(){
      this.innerHTML.bind(this.update());//I'm imagining you laughing at this line :)
    }
    
    Object.prototype.update = function(){
      alert('testing');
    }
    
    window.onload = function(){
      document.getElementById('containerDiv').initialize();//Make object into a container
    }
    </script>
    I appreciate your time
    Last edited by hddd12345678910; Dec 9, 2007 at 11:37. Reason: cut out irrelevant code

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone?

  3. #3
    SitePoint Member vidalstat's Avatar
    Join Date
    Dec 2007
    Location
    Brooklyn NY
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jQuery has a great plugin called live query that works really well for that sort of thing.

    http://docs.jquery.com/Tutorials:AJAX_and_Events


    talks about binding events and binding callbacks, etc. should be just what your looking for

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hddd12345678910 View Post
    Hi everyone,

    I'm trying to figure out a way to some how 'bind an event' to the innerHTML property. As an example, if I do something like:

    Code:
    <containerElement>.innerHTML='some HTML code';
    I would like somehow to be able to fire an event to accommodate the updated container content.
    This is not possible in this form, I'm afraid. In theory, this what "mutation events" like "NodeAdded" etc are for, but they are still not fully supported in major browsers. The practical solution would be to have a timer function watching for the content change.

    Code:
    <script>
    function $(s) { return document.getElementById(s) }
    
    setInterval(function() {
    	if(window._oldHTML != $('blah').innerHTML) {
    		window._oldHTML = $('blah').innerHTML;
    		contentChanged.call($('blah'));
    	}
    }, 100);
    
    contentChanged = function() {
    	alert(this.innerHTML);
    }
    </script>
    
    <div id="blah">foo</div>
    <button onclick="$('blah').innerHTML += ' bar'">test</button>

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much to the both of you for your time. I have finals starting in about...2 days, but I'll definitely look into it afterwards


Bookmarks

Posting Permissions

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