...

View Full Version : Adding OnClick property to dynamicly generated Images



orhor
07-04-2005, 11:55 AM
Hi,

this is my question, I wrote a script that generates new IMGs in my page. I need to add a onClick property to these newly generated images. I would like to have some javascript solution, that would work the same way like if the images would be written in HTML they had these tags: <img onClick='javascript: myFunction("thisImageId");'>

this is a part of my code:


function addNewImg(newImageId){
newImg = document.createElement('img');
newImg.id = "image"+newImageId;
newImg = document.getElementById("divimage1").appendChild(newImg);
document.getElementById("image"+newImageId).onclick=ppEdit("image"+newImageId,"image"); // this is the line that doesnt work for me
}
sorry for my english, any help will be greatly appreciated

jscheuer1
07-04-2005, 12:45 PM
function addNewImg(newImageId){
newImg = document.createElement('img');
newImg.id = "image"+newImageId;
newImg = document.getElementById("divimage1").appendChild(newImg);
var onC='ppEdit("image"'+newImageId+',"image")'
document.getElementById("image"+newImageId).onclick=new Function(onC);
}The way you have it, this will do what you say you want but, I find it hard to believe that you want the function 'ppEdit' to have the word 'image' as its second parameter. The word 'image' is not a variable in this code and it will only be the literal word 'image' in the onclick function.

orhor
07-04-2005, 01:05 PM
thank you very much.

-> the word "image": I know it, and I want it to act exactly this literal way :-)

glenngv
07-04-2005, 01:53 PM
Using Function constructor is sometimes confusing because of the use of single and double quotes and if the parameter is an object. Alternative solution is to use anonymous function.

document.getElementById("image"+newImageId).onclick=function(){
ppEdit(this.id, "image");
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum