getAttribute() and setAttribute() are often not required and can in some cases not be or do what one expects. Assuming you have the markup to support all this, I would:
Code:
function addImageToForm(formID) {
//get a reference to the form
var formElement = document.getElementById(formID);
//get a reference to the element that contains the url
var elementWithImageURL = document.getElementbyId("imageURLInput");
//get URL entered
var imageURL = elementWithImageURL.value;
//create image element
// ----------------------------------------------------------------------------------
var newImage = document.createElement("img");
//reference the source using the url
newImage.src = imageURL;
newImage.name = "newImage";
//append the image to form
formElement.appendChild(newImage);
}
If that still doesn't work, make sure the value of the element is as expected with an alert:
Code:
function addImageToForm(formID) {
//get a reference to the form
var formElement = document.getElementById(formID);
//get a reference to the element that contains the url
var elementWithImageURL = document.getElementbyId("imageURLInput");
//get URL entered
var imageURL = elementWithImageURL.value;
alert(imageURL);
//create image element
// ----------------------------------------------------------------------------------
var newImage = document.createElement("img");
//reference the source using the url
newImage.src = imageURL;
newImage.name = "newImage";
//append the image to form
formElement.appendChild(newImage);
}
If you want more help I need to see the markup you are using this with. I also need to know what event runs this function. Perhaps the expected elements aren't available yet.
Bookmarks