user1425871
user1425871

Reputation: 149

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
  }

Upvotes: 0

Views: 173

Answers (2)

Tharabas
Tharabas

Reputation: 3422

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

Upvotes: 1

John Conde
John Conde

Reputation: 219834

You need to wait until after the Ajax call to check the contents of that DIV:

new Ajax.Request(url, {
  method: 'get',
  onSuccess: function(transport) {
    if($('lesson-'+tab).innerHTML() == null)
    {
     //do the job
    }
  }
});

Upvotes: 1

Related Questions