SitePoint Sponsor

User Tag List

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

    dynamically bind function calls to named element

    My .jsp page has dynamically generated elements whose IDs start with "dw_licencelist_details_" and are ended with the row number, typically from 1 to 32. (i.e. dw_licencelist_details_32 would be the last element's ID).

    I would like the 'onmouseover' event to fire and report the row number in an alert box. (This is just a test) I tried this code, but it only ever reports the last row - not each row, as I'd hoped. Any ideas as to how to accomplish this?

    function f_postOnLoad (){

    var spans = new Array();
    // lv_rowcount is set earlier, contains the number of rows on the page.
    for (j = 1; j < lv_rowcount + 1; j++){
    spans[j] = document.getElementById('dw_licencelist_detail_' + j);
    spans[j].onmouseover = function(){
    changeBorder(j);
    }
    }


    }
    function changeBorder(rownumber)
    {
    alert("hello from row" + rownumber);
    }


    thanks

    Marvin

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this in your for loop:

    Code:
    spans[j].setAttribute('onmouseover','changeBorder(' + j + ')');

  3. #3
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a common problem. The solution is explained here: why does setting an onclick event trigger it?

    World Wide Weird, IE doesn't support using setAttribute() to add event handlers.

    P.S. It would be helpful if you put your code between [code][/code] tags in the future.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


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
  •