Search code examples
prototypejs

Check the content of a div with PrototypeJS


I use PrototypeJS and I need to check whether the content of a div is null or not. But the followng code gives an error. The div does not initially exist. It is added via an AJAX function. How can I check it?

if($('lesson-'+tab).innerHTML() == null)
  {
     //do the job
  }

Solution

  • innerHTML is a string property of the HTMLElement, and not a function so you will get an error when calling it. As it is a string, you do not check for null but for the string being empty. Additionally you might want to check, whether the element exists in order to prevent an error when result of the $ function is null. And, as John Conde already mentioned, you need to check after the AJAX call is done:

    new Ajax.Request(url, {
      onComplete: function(transport) {
        var lessonTab = $('lesson-' + tab)
        if (!lessonTab || lessonTab.innerHTML.length === 0) {
          // do stuff
        }
      }
    })