OtagoHarbour
OtagoHarbour

Reputation: 4183

Trying to Position Form with JavaScript

I am trying to use JavaScript to create a form and position it. I use the following code.

var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");

// Position form
f.setAttribute('offsetTop',offsetTop);
f.setAttribute('offsetLeft',parseInt(offsetLeft)+parseInt(imageWidth));

document.getElementsByTagName('body')[0].appendChild(f);

However, when I examine the DOM using Firebug, the position of the form is the same as if I had not use the form positioning calls at all. That is, it is beneath the last element which is an image.

Upvotes: 0

Views: 1356

Answers (2)

Mihail
Mihail

Reputation: 1479

offsetTop and offsetLeft is not CSS property. And you need use not static position. Use "style":

f.style.position = "absolute";//or relative, fixed
f.style.top = offsetTop+"px";//set css top

Upvotes: 1

jessegavin
jessegavin

Reputation: 75650

Try this.

var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");

// Position form
f.style.position = "absolute";
f.style.top = offsetTop +"px";
f.style.left = (parseInt(offsetLeft)+parseInt(imageWidth)) +"px";

Upvotes: 2

Related Questions