Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Feb 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •