SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Ajax issue

  1. #1
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    Montreal
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax issue

    Hi,

    when I first load a page, I set the onchange of a dropbox field like this in js :

    Code:
    // Variable
    var color = document.getElementById('color');
    
    // Set color action
    if(color){ color.onchange = xColor; }
    Everything work but when if I use Ajax to update the div that contain the dropbox that have the id="color" then it is like if the onchange does not work anymore...

    Is it normal?

    Thank

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    That's expected if the element that's being updated is being replaced instead.

    You should remove the onchange event before updating, update the element then set the onchange event again. This helps to prevent memory leaks from occuring.

    Code javascript:
    var el;
    var elEvent;
     
    el = document.getElementById('color');
    elEvent = el.onchange;
    el.onchange = null;
    // do the ajax update
    ...
    // and then
    el.onchange = elEvent;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    Montreal
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank alot .... it work now


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
  •