I am having a lot of trouble trying to access elements inside the 'hmcontent' iframe used by H&M templates to build HTML pages. Currently I am using the 'WebHelp, iFrames Responsive Blue.hmskin' template on v7.5.4.4760.
The problem is that I created a simple feedback form for each page and I want to use some javascript code to control that form. To do so, I created a javascript file that is loaded with the pages and it succeeds to get a reference to the 'hmcontent' iframe, but when I try to get a reference to the elements inside that iframe, these references always return null . The next example code shows how this is working now:
Code: Select all
var content_frame = document.getElementById("hmcontent").contentDocument? document.getElementById("hmcontent").contentDocument: document.getElementById("hmcontent").contentWindow.document;
if (content_frame === null) {
console.log("Content frame is null");
} else {
console.log("Content frame is not null"); // This message always show, so I am OK here
}
// The element inside hmcontent is <a id="link-good"></a>
// So I try to get a reference to this element
var btn_good = content_frame.getElementById("link-good");
// And here I try to add a listener to the element
// But I always receive a message
// Cannot read property 'addEventListener' of null
btn_good.addEventListener("click", function(){
console.log("Clicked good");
});
Thank you.