(EDITED: code syntax, and yeah, I forgot to remove the extra stuff for the with statement.)
First things, first, you used a closing div tag to close your img, which is wrong.
For your own sake and others, it is recommended that the first letter of each word in a variable is capitalized except for the first word in the variable. For example: floattext becomes floatText. For object variables, I like the Java convention of capitalizing the first word as well, ie: MyObject.
Also, try not to use backslashes (\) to run more of a string into a variable. Instead use the += operator for reasons of usefulness and to avoid some bugs in some browsers.
Here's two ideas that I think might be what you are trying to get at.
#1:
Code:
var floatText = []; //The "[]" is shorthand for "new Array()"
function setFloatText(index, uri) {
floatText[index] = "<img src='"+uri+"'";
floatText[index] += " style='width:288px;height: 216px; float: top;";
floatText[index] += " margin-left: 5px; border:1px solid black' />";
}
#2:
Code:
var floattext = [];
function setFloatText(index, uri) {
floatText[index] = document.createElement("img");
with(floatText[index]) {
src = uri;
style.width = "288px";
style.height = "216px";
style.cssfloat = "top";
style.marginLeft = "5px";
style.borderWidth = "1px";
style.borderStyle = "solid";
style.borderColor = "#000000";
}
}
With #2, to use the img element the code created, you would want to use the insertBefore() built-in function DOM provided. The syntax is:
parentElement.insertBefore(newElement, referenceElement)
where the newElement, in this case your img, would be inserted before the referenceElement.
In the case where you want to add the newElement to after all other elements within the parentElement, use the appendChild(). The syntax is:
parentElement.appendChild(newElement)
The #2 code is more versitile, and is the better way of going, but if you don't understand it, try #1.
A working example for#2 could be like:
Code:
/* Assume above #2 code is in here */
setFloatText(3, "http://www.google.com/intl/xx-hacker_ALL/images/logo.gif");
var x = document.getElementsByTagName("body")[0];
x.insertBefore(floatText[3], x.firstChild);
Bookmarks