/    Sign up×
Community /Pin to ProfileBookmark

Positioning absolute div

I have the following JavaScript/CSS which creates a div with some content. The goal (works in FF, not in IE) is to have it position the div at the passed x/y coordinates (x/y is either pageX/clientX depending on browser). However, under IE, it always positions the div at the end of the parentID element (a form element in this case) ignoring the element.style.left and element.style.top properties.

Anyone know how to get IE to behave like FF for this operation?

[code=html]
.absoluteHidden,
.userNoteDivClass {
position:absolute;
visibility:hidden;
}
.userNoteDivClass {
background-color:#FFCC33;
border:2px solid red;
padding:10px 10px 10px 20px;
}
.userNoteDivClass input {
width:400px;
}
.userNoteDivClass h1,
.userNoteDivClass h2 {
text-align:center;
}
[/code]

[CODE]
function createAbsoluteElement(parentID, containerType, name, useClass, x, y, xAdjust) {
var parentTag = document.getElementById(parentID);
winWidth = docWidth();
var newEl = document.createElement(containerType);
newEl.setAttribute(‘id’, id = formNameToID(name));
if( useClass )
newEl.setAttribute(‘class’, useClass);
newEl.style.left = ((x+xAdjust) > winWidth) ? (winWidth-xAdjust) : x;
newEl.style.top = y;
parentTag.appendChild(newEl);
return newEl;
}

function createNoteDiv(parentID, name, noteTitle, value, x, y, readOnly) {

newEl = createAbsoluteElement(parentID, ‘div’, ‘div’+name, ‘userNoteDivClass’, x, y, xAdjust=400);

ro = ”;
if( readOnly )
ro = ‘readonly’;
innerStr = ‘<h2>’+noteTitle+'</h2>’;
innerStr += ‘<input type=”text” name=”‘+name+'” value=”‘+value+'” ‘+ro+’ />’;
innerStr += ‘<br/><br/>’;
innerStr += ‘<a href=”#” onclick=”hideNote(”+id+”);return false;”>Click To Close</a>’;
newEl.innerHTML = innerStr;
return newEl;
}

function showNote(parentID, name, noteTitle, x, y, readOnly) {
hiddenID = formNameToID(name);
divID = ‘div’+hiddenID;
// Now see if it exists… If not, create it and set the appropriate values.
if( tag = document.getElementById(divID) ) {
// exists, just show it.
tag.style.visibility = ‘visible’;
} else { // doesn’t exist
// Get the existing value from the hidden element
hiddenTag = document.getElementById(hiddenID);
val = ”;
if( hiddenTag ) {
val = hiddenTag.value;
// remove the original hidden element. It’s replaced by the dynamic one (name anyway)
hiddenTag.parentNode.removeChild(hiddenTag);
}
newDiv = createNoteDiv(parentID, name, noteTitle, val, x, y, readOnly);
newDiv.style.visibility = ‘visible’;
if( document.forms[parentID] ) {
focusItem = document.forms[parentID].elements[name];
if( focusItem && focusItem.type != ‘hidden’ && focusItem.style.display != ‘none’ && !focusControl.disabled ) {
focusItem.focus();
focusItem.select();
}
}
}
return false;
}

[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorApr 08.2008 — <i>
</i>newEl.setAttribute('class', useClass);

For IE [I]class[/I] is a reserved word, thus it uses setAttribute() in a different manner. But you can use rather the trusty good DOM 0 crossbrowser syntax:
<i>
</i>newEl.className=useClass;
Copy linkTweet thisAlerts:
@tbirnsethauthorApr 08.2008 — Not sure that it's the class specification that is the problem. After the setAttribute('class', useClass), the 'top' and 'left' positions are set. These are what seem to be being ignored in IE.
Copy linkTweet thisAlerts:
@tbirnsethauthorApr 08.2008 — Actually it does... Since IE seemed to puke on the setAttribute('class', useClass), it never got the definition of position:absolute. Hence the subsequent assignements of x/y positioning were ignored.

thank you.
×

Success!

Help @tbirnseth spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...