Forum Moderators: open

Message Too Old, No Replies

Identify parent container from inline script

Manipulate parent item / element without a function

         

frobozz

5:16 am on Sep 16, 2011 (gmt 0)

10+ Year Member



This is going to sound strange, so allow me to preemptively say that it is for a project that will run only on a local (non-web-server) INTRAnet, and as such I'm without the power of PHP, Ajax, etc.

I have a DIV, which needs a unique background image assigned. The URL of the background image requires a dynamic path prefix, which I've acquired as a JavaScript variable.

Normally we would do something like:

document.getElementById('my_div').style.backgroundImage = "url(" + path_var + "'/image_name')";


But I am not willing to assign IDs to any of these DIVs. (This code will be huge, and will be frequently edited and sorted. IDs will make a mess of it.)

No big deal, on any other day I could just use:

this.style.backgroundImage = 'something';


... if I were attached to some sort of event. But I'm not. The image needs to be set immediately as the page loads. Using a mouseover/mouseout/etc. does no good -- the DIV would have no background until / unless someone moused over it.

So what I need is this:

<div>
<script>
this.style.backgroundImage = 'something';
</script>
</div>


I've tried replaceing 'this' with 'parentNode', appending 'parentNode', juggling all sorts of instances of 'document,' etc. but to no avail.

I suspect that what I want is not possible -- to reference and manipulate a container without an ID from an inline script. (To clarify -- I cannot create a function, because I know of no method of calling it for each DIV in question. If there is such a method, I will be thrilled!)

birdbrain

7:04 am on Sep 16, 2011 (gmt 0)



Hi there frobozz,

would document.getElementsByTagName('div') be of any use for your project?
This returns an array of all div elements in the document.

birdbrain

penders

11:58 am on Sep 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you can describe in 'human terms' the location of this DIV in the document then you should be able to target this DIV in JavaScript. Either by getElementsByTagName(), as birdbrain suggests, or by some other DOM methods.

For example, "the 5th DIV following an unordered list with class 'magic', that contains 2 paragraphs with the words 'hello world' in the 2nd!?"

Fotiman

1:23 pm on Sep 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here's an alternative approach to consider. Give your div(s?) a classname if multiple divs will have the same background image, or an id if each one is unique (I know you said you didn't want to use ID's because it would "make a mess" of editing and sorting, but isn't having inline scripts scattered throughout the markup going to be much more of a mess?). Then instead of trying to write out the background property with JavaScript, write out a style element instead which contains the background for the class/id.