Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-04-2005, 10:55 AM   PM User | #1
orhor
New to the CF scene

 
Join Date: Feb 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
orhor is an unknown quantity at this point
Adding OnClick property to dynamicly generated Images

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:

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
orhor is offline   Reply With Quote
Old 07-04-2005, 11:45 AM   PM User | #2
jscheuer1
Regular Coder

 
Join Date: Mar 2005
Location: SE PA USA
Posts: 373
Thanks: 0
Thanked 0 Times in 0 Posts
jscheuer1 is an unknown quantity at this point
Code:
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.
jscheuer1 is offline   Reply With Quote
Old 07-04-2005, 12:05 PM   PM User | #3
orhor
New to the CF scene

 
Join Date: Feb 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
orhor is an unknown quantity at this point
thank you very much.

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

Last edited by orhor; 07-04-2005 at 12:22 PM..
orhor is offline   Reply With Quote
Old 07-04-2005, 12:53 PM   PM User | #4
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Alternative solution

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.
Code:
document.getElementById("image"+newImageId).onclick=function(){
  ppEdit(this.id, "image");
}
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:52 AM.


Advertisement
Log in to turn off these ads.