Forum Moderators: open

Message Too Old, No Replies

Adding OnClick property to dynamicly genertated Images

Adding OnClick property to dynamicly genertated Images

         

orhor

9:48 am on Jul 4, 2005 (gmt 0)

10+ Year Member



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

RonPK

10:22 am on Jul 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a wild guess, but it may work:

newImg.onclick = ppEdit("image"+newImageId);

I'm not so confident about passing parameters to event handlers, but you'll soon notice if that causes problems ;)

orhor

10:45 am on Jul 4, 2005 (gmt 0)

10+ Year Member



thanks for answering, but unfortunatly this doesn't work...

orhor

10:58 am on Jul 4, 2005 (gmt 0)

10+ Year Member



there are some strange things I cant understand:

when I use this:

function klik(){
alert("ppp");
}

function addNewImg(newImageId){
newImg = document.createElement('img');
newImg.id = "image"+newImageId;
newImg = document.getElementById("divimage1").appendChild(newImg);
newImg.onclick=klik
}

it works. The new image is created and after clicking on it "ppp" is alerted.

but when I use this:

function klik(ff){
alert(ff);
}

function addNewImg(newImageId){
newImg = document.createElement('img');
newImg.id = "image"+newImageId;
newImg = document.getElementById("divimage1").appendChild(newImg);
newImg.onclick=klik("ppp");
}

"ppp" is alerted in the moment of executing the statement newImg.onclick=klik("ppp"); and when clicking on the generated image nothing happens, even Javascript Console of Firefox stays blind.

I googled a bit and found that the first example shoul be used, but I need to use some argument to be able to edit the generated image later. And what if there will be 10 generated images?

orhor

11:10 am on Jul 4, 2005 (gmt 0)

10+ Year Member



so I got the solution from jscheuer1 at codingforum.com:

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);
}

it works

orhor

1:12 pm on Jul 4, 2005 (gmt 0)

10+ Year Member



alternative solution:

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

RonPK

2:51 pm on Jul 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you worked it out. I told you that passing parameters to event handlers can get messy ;)

webaster

4:00 pm on Jul 4, 2005 (gmt 0)

10+ Year Member



maybe an object detection be great to add too
why?
So browsers that understand document.getElementById,
document.createElement execute the code.


function addNewImg(newImageId){
//object detection check
if (!document.getElementById &&!document.createElement){return;}
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);
}

alternative solution:
if (!document.getElementById) {return;}
document.getElementById("image"+newImageId).onclick=function(){
ppEdit(this.id, "image");
}

webaster

4:02 pm on Jul 4, 2005 (gmt 0)

10+ Year Member



forgot a whitespace there:

if (!document.getElementById &&!document.createElement){return;}

webaster

4:26 pm on Jul 4, 2005 (gmt 0)

10+ Year Member



forgot a whitespace there:

if (!document.getElementById &&!document.createElement){return;}

webaster

4:27 pm on Jul 4, 2005 (gmt 0)

10+ Year Member



sorry for that it is the editor I guess

orhor

7:59 am on Jul 7, 2005 (gmt 0)

10+ Year Member



thanks to everyone of you