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:

<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:D ) script:

<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 :slight_smile:

Anyone?:frowning:

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

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.


<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>

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 :slight_smile: