www.webdeveloper.com
Results 1 to 6 of 6

Thread: onclick problem

  1. #1
    Join Date
    Jun 2006
    Posts
    61

    onclick problem

    Can anyone tell me what I am doing wrong here:

    for(var y=0;y<8;++y)
    for(var x=0;x<8;++x) {
    ...
    var thisObj = eval('document.'+String(x)+String(y));
    thisObj.src = 'myImage.png';
    thisObj.onclick = 'myFunc('+String(x)+','+String(y)+')';
    ...
    }

    the problem is that while the .src property is successfully set, the .onclick property isn't (i.e. I see the proper image, but clicking on it does nothing).

    Thanks,

    JAS

  2. #2
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,277
    Try
    Code:
    var thisObj = document[String(x) + String (y)];
    thisObj.src = 'myImage.png';
    thisObj.onclick = function () {myFunc(String(x), String(y)};
    You should pretty much never use "eval" and the handlers take functions not strings.

  3. #3
    Join Date
    Jun 2006
    Posts
    61
    Well, that's a step in the right direction, but...

    The point is to report the column and row numbers of a table cell when the user clicks on that cell.

    Your code calls my function, but always reports the same arguments, i.e. not the column and row numbers I want.

    I even tried this:

    thisObj.onclick = function() { myFunc(thisObj.id) };

    but that always showed the same string as well.

    Any (more) ideas?

    JAS

  4. #4
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,277
    If you use thisObj.onclick = myFunc then inside "myFunc" you can access the object with "this". You should be able to make your function work with that.

  5. #5
    Join Date
    Jun 2006
    Posts
    61
    It works! Thanks very much.

    JAS

  6. #6
    Join Date
    Jun 2006
    Posts
    61
    Something odd: unless I put myFunc inside a setTimeout call, things do not go well at all. There is no end of the strangeness. For instance, without the setTimeout, even FireBug gets confused, and its trace function skips lines for not reason at all. I don't know why it occurred to me that using setTimeout would help, but it does, so that's all to the good. Thanks again.

    JAS

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles