I would like to write a greasemonkey script to disable a div on a certain page.
On any given load of the page I don’t know where in the DOM the div will be but I know it’s always called <div id = "alertPanel"> ....</div>
How would I go about disabling this div?
My intial thoughts were something along the lines of:
var myDivs= document.getElementsByTagName('div');
for (i=0; i<myDivs.length; i++)
{
if (myDivs[i].<get id property somehow> = "alertPanel")
myDivs[i].style.visibility = 'hidden';
}
but as you can tell I’m stuck at accessing the id property for an equality check.
Incidently, I’m using a text editor to write this – I guessing that a standard javascript editor would give an autocompletion list after typing in myDivs[i].
If it has an
id, you can usedocument.getElementById:Then if it exists, you can either remove it (probably a bad idea) or hide it:
Update: Re your comment on another answer:
If the element is in an
iframe, then you have to callgetElementByIdon the document that’s in theiframe, sinceiframes are separate windows and have separate documents. If you know theidof theiframe, you can usedocument.getElementByIdto get theiframeinstance, and then usecontentDocumentto access its document, and then usegetElementByIdon that to get the “minigamesContainer” element:(The
try/catchis there because of a potential security error accessing the content of the iframe; I don’t know Greasemonkey well enough to know whether the SOP applies to it. I tend to assume it doesn’t, but better safe…)If you don’t know the
idof theiframeor if it doesn’t have one, you can just loop through all of them by getting them withdocument.getElementsByTagNameand then looping:References: