-1

I want to change the div's style which is located in the parent of a parent. I am able to access them via DOM, but how can I change its CSS style using JQuery?

Selection code:

window.parent.parent.parent.document.getElementsByTagName('code').

I would love to only change a div that doesn't have display:none already set, but I really don't care if we change only one, or all of them.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Alex Blokha
  • 1,141
  • 2
  • 17
  • 30
  • Why jQuery when you already have it in JS? Simple look and set display...`var nested = window.parent.parent.parent.document.getElementsByTagName('code'); for(var i=0;i – epascarello Mar 03 '14 at 16:45
  • you can change style with [.css()](http://api.jquery.com/css/) – MamaWalter Mar 03 '14 at 16:46
  • `window.parent.parent.parent.document.getElementsByTagName('code').style.display = 'block';` – Awlad Liton Mar 03 '14 at 16:49
  • @AwladLiton Wrong. This function returns a list of elements. –  Mar 03 '14 at 16:50
  • Remove your comments @AmitJoki, better not to interfere with the wall's question. –  Mar 03 '14 at 17:19
  • http://stackoverflow.com/questions/18743846/select-grand-parent-element-attribute, http://stackoverflow.com/questions/3730035/how-to-change-css-using-jquery – RandomSeed Mar 09 '14 at 03:33

1 Answers1

1

You can always use the context parameter on $( selector [, context ] )

$("[name=code]",parent.parent.parent.document).css('display','none');

If you want to only change the one that are visible you can use the is function in JQuery in order to filter:

$("[name=code]",parent.parent.parent.document).is(':visible').css('display','none');

JQuery page: https://api.jquery.com/jquery/

Garis M Suero
  • 7,974
  • 7
  • 45
  • 68
  • 1
    1) $("[name=code]",parent.parent.parent.document) This returns 0 occurences. But I've written such line $(window.parent.parent.parent.document).find('code') which returns correct number of occurences. 2) .is(':visible') This returns true or false, so you can not apply css to it. P.S. I've found the solution. – Alex Blokha Mar 05 '14 at 10:08