So I’m doing a redesign for a site with an unhelpful CMS where I don’t have full access to the markup (or ftp access). There’s a node in particular which is hindering my progress significantly – its written with inline style & no class or ID and I have to change it (not remove it). Its looking like:
<div style="background-color: blue">
<div class="editablecontent">
(stuff I can edit in the CMS goes here)
</div>
</div>
I don’t think getElementsByClassName is going to work here? but what sort of works is the very ugly defining an empty div with an ID then and document.getElementById.parentNode.parentNode.style.backgroundColor=”white” which is obviously filthy and doesn’t work in IE anyways and I need IE8 support (at least). I am not using a framework but do have access to the header.
Thanks in advance.
Assuming this content is unique in your document (e.g. there’s only one of them), you can use this to find it:
This does the following steps:
classNameof"editablecontent"If you wanted any further checks based on other criteria to make sure you were finding the right object, you could add those to the logic, though these checks are all you have disclosed to us.
Working example: http://jsfiddle.net/jfriend00/NmxUn/
Another approach is to use
getElementsByClassNameand install a polyfill like this one to make it work in older browsers:And, then you could simply do this, even on older versions of IE: