views:

290

answers:

2

I am trying to create an overlay for a div. I am attempting to accomplish this using JQuery. This what the JS looks like:

 $('#container').wrapInner('<div />')
        .css('opacity', '0.5')
        .css('z-index', '2')
        .css('background','black')
        .attr('id','overlay')
    }
};

Update: This is the markup before the JS function is applied:

<body>
<form id="form1" runat="server">
<div id="header">My Header</div>
<div id="container">Container</div>
<div id="menu">
 <input type="text" />
 <input type="button" class="button" value="Go" onclick="overlay.toggleOverlay();return true" />        
</div>  
</form>

This seems to manipulate the 'container' div in a way I would not have expected. This is what the markup looks like after the wrapInner method is run:

<div id="overlay" style="z-index: 2; filter: alpha(opacity=50); ZOOM: 1; background: black;">
<div>ContainerDiv</div>
</div>

It is as if the wrapInner method has striped the container div of its attributes. Can someone suggest to me a better way of doing this?

Thanks..

+1  A: 

The call to "wrapInner" returns the jQuery object that you created with $('#container'), not the div that wraps the container. Try this:

 $('#container').wrapInner('<div />').children()
    .css(/* ... */)
    .css(/* ... */)
    .attr('id', 'overlay');

[edit] actually I have no idea what you expect to happen so I'm not sure whether my example makes any sense.

[edit again] OK if you want to wrap the container then you don't want to use "wrapInner()". Just use "wrap", and then call .parent() and add your CSS and "id" value.

Pointy
Thanks.. this wrapped the child text node ("Container"). It did, however, leave the container div inact. What I need is to wrap the container div (<div id='container'>) with this new ovelay div.
Nick
Your right. However the overlay width is larger than that of the container. How do I get these to match up? Setting the overlay width (offset().width) equal to the container's doesn't work and it seems to left justify the container.
Nick
A: 

The Wrap() method is what I needed

 $('#container').wrap("<div id='overlay'> </div>")
        .css('opacity', '0.5')
        .css('z-index', '2')
        .css('background','black')

Update:

In the end it decided to not wrap the container div with the overlay. The styling was getting tricky so I just decided to make the overlay div a sibling of container and then position it over the container. I am using offset values of the container to do this.

var left = $("#container").offset().left;
        var top = $("#container").offset().top;
        var width = $("#container").outerWidth();
        var height = $("#container").outerHeight();
        $("#overlay").css({ "left": left + "px", "top": top + "px", "width": width + "px", "height": height + "px", "opacity": "0.5" });
Nick
Just keep in mind that the changes to the css that you perform happen to the `#container` div and not the `#overlay` one ..
Gaby
Ya.. it appears that my container is inheriting from the overlay.. I think these divs will have to be siblings.. ahhhI only went down this road because I was having trouble getting the overlay positioned correctly using the container's offset values.
Nick
Call .wrap('<div/>'), then string a call to .parent(). The call to "wrap" will create the outer div, and then the call to parent() will make it the subject of the jQuery object. Then you can call your css routines and the right thing will happen.
Pointy
@Pointy: This has me very close. Thank you. The only issue that remains is that the overlay is very wide. I am trying to set the overlay CSS like this: .css('width', $("#container").width())For some reason the overlay is still too wide and the container has shifted to the left. Argg Almost there! :)
Nick