SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem in onclick function

    Hi,

    I have a table in which rows are dynamically created. I have declared a onclick function dynamically which has arguments in it. And the arguments changes correspondingly as a result of xmlhttprequest.

    How to assign a onclick function with arguments for a dynamically created element?

    The problem is dynamically declared onclick function doesn't take the arguments.

    My code is below:
    Code:
    tr=document.createElement('tr');
    for(j=1;j<=3;j++)	
    {
    td=document.createElement('td');
    img = document.createElement('img');
    img.id = "status" + (i+1);
    img.src = '<?php echo $SG_ROOT_PATH; ?>' + /images/latest.gif;
    img.onClick = function() { ViewCurrentState(studentName, Id, age);
    td.appendChild(img);
    tr.appendChild(td);					document.getElementById('retrieveBkupsTable').appendChild(tr);
    
    function ViewCurrentState(studentName, Id, age)
    {
         alert(studentName);
         alert(Id);
         alert(age);
    }
    Im getting undefined alert for all studentName, Id and age.

    Regards,
    viji.

  2. #2
    SitePoint Member GreboGuru's Avatar
    Join Date
    Jun 2006
    Location
    Wensleydale, UK
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    By the looks of it you're not passing in any values into the ViewCurrentState() function?

    Hope this helps

    Scott

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Test this, it should be fine:
    Code:
    var studentName = "Jim";
    var Id = "80881088";
    var age = "12";
    
    img.onclick = function() {
    	ViewCurrentState(studentName, Id, age);
    }
    Note the lowercase C in onclick. Yes it makes a difference.


Bookmarks

Posting Permissions

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