Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
104
rated 0 times [  110] [ 6]  / answers: 1 / hits: 8669  / 5 Years ago, wed, february 13, 2019, 12:00:00

I am trying to modify the TrustPilot widget on my site.



The basic structure of the TrustPilot widget is:



#tp-widget_wrapper .tp-widget-wrapper
#wrapper-top .wrapper-top
#tp-widget-reviews-wrapper .tp-widget-reviews-wrapper hidden-element
#tp-widget-reviews .tp-widget-reviews
.tp-widget-review
.tp-widget-stars
.date


I am trying to hide the div .date.



What I have gotten so far is:



var trustpilot_children = document.getElementById(trustpilot_widget).childNodes;
var trustpilot_frame = trustpilot_children[0];
var date_tags = trustpilot_frame.document.getElementsByClassName(date);
date_tags.style.display = none;


However I get the error:



Uncaught TypeError: Cannot read property 'getElementsByClassName' of undefined


The var trustpilot_frame is finding the iframe, I just can't access anything within it.



Edit



var trustpilot_children = document.getElementById(trustpilot_widget).childNodes;
var trustpilot_frame = trustpilot_children[0].contentDocument;
console.log(trustpilot_frame);
var date_tags = trustpilot_frame.getElementsByClassName(date);
console.log(date_tags);
date_tags.style.display = none;


Console:



enter



Edit 2



var trustpilot_children = document.getElementById(trustpilot_widget).childNodes;
var trustpilot_frame = trustpilot_children[0].contentDocument;
var style_tag = trustpilot_frame.createElement(style);
style_tag.textContent = .date{display:none;};
trustpilot_frame.getElementsByTagName(head)[0].appendChild(style_tag);
console.log(trustpilot_frame);


Console:



enter



Edit 3



var trustpilot_children = document.getElementById(trustpilot_widget).childNodes;
var trustpilot_frame = trustpilot_children[0].contentDocument;
trustpilot_frame.onload = setTimeout(hide_dates, 10000);
function hide_dates(){

// method 1
var style_tag = trustpilot_frame.createElement(style);
style_tag.textContent = .date{display:none;};
trustpilot_frame.getElementsByTagName(head)[0].appendChild(style_tag);
console.log(trustpilot_frame);

// method 2
var date_tags = trustpilot_frame.getElementsByClassName(date);
console.log(date_tags);
date_tags.style.display = none;
}


Console shows the same as EDIT2 for the dom and the first EDIT for date_tags



https://codepen.io/phallihan/pen/OdwgGd


More From » html

 Answers
4

To get the document of an iframe you need to use document.getElementById('iframe').contentDocumentyou should then be able to use getElementsByClassName


https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/contentDocument


Your updated code would look like:


var trustpilot_children = document.getElementById("trustpilot_widget").childNodes;
var trustpilot_frame = trustpilot_children[0];
var date_tags = trustpilot_frame.contentDocument.getElementsByClassName("date");
date_tags.style.display = "none";

Update


Try the below to wait for the iframe to load.


var trustpilot_children = document.getElementById("trustpilot_widget").childNodes;
var trustpilot_frame = trustpilot_children[0];
trustpilot_frame.onload = function() {
var date_tags = trustpilot_frame.contentDocument.getElementsByClassName("date");
date_tags.style.display = "none";
}

[#8951] Monday, February 11, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryderalfonsos

Total Points: 655
Total Questions: 88
Total Answers: 91

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
ryderalfonsos questions
Mon, Sep 9, 19, 00:00, 5 Years ago
Tue, Feb 12, 19, 00:00, 5 Years ago
Fri, Dec 28, 18, 00:00, 6 Years ago
;