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
    Mar 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Creating mouseover events for dynamic links in JS

    My application reads an array of URLs in Javascript and displays them in a table. I need to create mouseover events for each of the links (just an alert message for now.) I have tried this a few ways, but for each one the mouseover event fires for each link before anything else is loaded on the page, and when the page is loaded, no link is displayed. Here are the two ways I've tried:

    Code:
     
    var cell1 = document.createElement("TD");
    
    cell1.innerHTML = '<A HREF= ' + url + 'onMouseOver="' + alert("my alert box"); + '"> my page </A>';

    Code:
    cell1.innerHTML = '<A HREF= "' + url +  '"> my page </A>';
    cell1.onMouseOver = alert('alert');
    I have also tried many variations of these including:
    Code:
    cell1.innerHTML = '<A HREF= "' + url +  '"> my page </A>';
    cell1.onMouseOver = function(){alert('alert');};

    Code:
     
    var cell1 = document.createElement("TD");
    
    cell1.innerHTML = '<A HREF= " ' + url + ' " onMouseOver=" alert("my alert box"); "> my page </A>';
    But they all yield the same result, so any help would be much appreciated!

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    use event listeners.
    PHP Code:
    cell1.innerHTML '<A href="' url +  '"> my page </A>';
    cell1.firstChild.addEventListener("mouseover", function(){alert('alert');}, false); 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    pj221 (03-12-2010)

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you!

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    note of caution, IE uses attachEvent() instead of addEventListener() to guarantee future incompatibility problems
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Tags for this Thread

    Posting Permissions

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