page include data('object') issues

I have used this paradigm many times to access a page title and display messages:

var pageTitle = $('#ContractErrors'); <br>var editor = pageTitle.data('object').editor;<br>editor.handleMessages([{message: 'Pizza is good!', severity:'ERROR'}]);

I am trying to use this code inside of a page include. When I hit the second line, in the page include, I am getting an error that ‘Cannot read property “editor” of undefined’. Obviously the pageTitle variable is missing something when it’s running in a page include. The page works perfectly when it’s running by itself. Any ideas why this would happen? I have checked that there are no naming conflicts between the pages. Any ideas?



Since this is a Page Include, I’d guess that the problem is that the Page Title hasn’t been rendered to the page yet when your code runs. This code should be run within a block like this:

$(document.body).one(‘pageload’,function(){
    var pageTitle = $(‘#ContractErrors’); 
    var editor = pageTitle.data(‘object’).editor;
    editor.handleMessages([{message: ‘Pizza is good!’, severity:‘ERROR’}]);
});

That should ensure that your Page Title has been rendered. If your Page Title hasn’t been rendered yet, then .data(‘object’) will return undefined, because the jQuery selector $(‘#ContractErrors’) will return an empty array of elements.

Thanks for the quick response zach! The thing is that I’m running this in a regular in-line snippet, however it is inside of a wizard which is inside of a page include. Basically the snippet runs between step3 and step4 of a wizard. I actually am not getting an empty array returned, more like an element that’s missing something…  

This is what the pageTitle variable looks like:

If the Page Title element is in the DOM, then you should be able to do this:

var pageTitle = $(‘#ContractErrors’);
console.log(pageTitle.length);

and get 1 returned in the console



It’s really weird because I’m getting 0 in the console, but I’m 100% sure that it’s in the DOM. It works fine when it’s not in the page Include…

Sorry it was my bad, I’ll never doubt skuid again…

I had a line that closed popups, which ended up closing the popup that the page include was in which made its DOM disappear. Thanks anyway!