Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
how to apply body.addClass("className") to iframe from the parent document

I have a WP page with embedded iframes. There is a widhet that changes the style of the page, and I need that it also changes the style of all embedded iframes. The style changes by clicking the button that has the following event handler:

NightVersionButton.click(function (event) {
    // this is a part that changes the page style (and it works)
    event.preventDefault();
    Cookies.set('contrast', 'night', { expires: 7, path: window.cookiePath });
    body.removeClass('highcontrast highcontrast2 highcontrast3');
    body.addClass('night');
    
    // this is a part that should change the style of all iframes, but it does not work due to the error "Uncaught TypeError: single_iframe.contentDocument.body.addClass is not a function"
    var all_iframes = document.getElementsByTagName('iframe');
    for (let single_iframe of all_iframes) {
        var iframe_doc = single_iframe.contentWindow ? single_iframe.contentWindow.document : single_iframe.contentDocument;
        iframe_doc.body.addClass('night');
        //$(single_iframe).contents().find('body').addClass('night'); // this jQuery alternative does not work neither
    }

});

However, the part of the JS code responsible for changing the styles in all iframes does not work, with the console error "Uncaught TypeError: single_iframe.contentDocument.body.addClass is not a function".

Please could you suggest the correct solution on how to change the JS that it will also apply addClass to all iframes?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda