Results 1 to 5 of 5

Thread: Killin me - Javascript!

  1. #1
    charlesh's Avatar
    charlesh is offline Master Glow Jedi
    Join Date
    Aug 2006
    Location
    Atlanta, GA - better than you imagined it would be.
    Posts
    189

    Default Killin me - Javascript!

    OK,

    So it's been a while, but I've been really busy. So, I think I've got some of this Ajax stuff figured out, but I'm really stumpped right now, so I thought I would post this before I pull my hair out. Basically, I'm trying to disable a div element with a bunch of stuff in it after getting a sucessful ajax response. It seems to work if I write it like this:

    Code:
     
    var id = o.responseText;
       if (document.getElementById('1').hasChildNodes())
      {
          while ( document.getElementById('1').childNodes.length >= 1 )
          {
              document.getElementById('1').removeChild( document.getElementById('1')s.firstChild );       
          } 
      }
    But, you see my problem is having to pass a variable ID to the element, so I need a:

    Code:
     
    var div = "document.getElementById("+id+")";
    ... to pass the id back from the response. Now, simple, right? But when I write this code, nothing works:

    Code:
     
    var id = o.responseText;
     var div = "document.getElementById("+id+")";
     
      if (div.hasChildNodes())
      {
          while ( div.childNodes.length >= 1 )
          {
              div.removeChild(div.firstChild );       
          } 
      }
    The error in firebug says that div.hasChildNodes() is not a function! Why isn't the string being passed????? AHGRRRHH!!!

    Hacking away...

  2. #2
    charlesh's Avatar
    charlesh is offline Master Glow Jedi
    Join Date
    Aug 2006
    Location
    Atlanta, GA - better than you imagined it would be.
    Posts
    189

    Default

    OK, Figured it out:

    Instead of this:
    Code:
    var div = "document.getElementById("+id+")";
    I needed to remove the quotes:
    Code:
    var cell = document.getElementById(id);
    Javascript!!! Can be very frustrating - almost impossible to debug!

    Thanks for listening...

  3. #3
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    Why wasnt I here sooner? I saw the prob in 5 seconds. Sorry!
    PS. Try that in php! <g>

    No language I know of lets you mix quotes like that without escaping them, and error 2 is, even if you did escape the inners, you are asking for a string containing +id+
    A simple alert(div) should have produced an error. Course, you have to know what to look for.
    div.hasChildNodes() is not a function!

    Why isnt it a function? Probably because div doesnt exist.

    Yeah JS is a bitch, and Firebug is the ****!

    I takes a whule to believe this, but when you do you will be a master Javascrip Jedi as well as a master glow Jedi.

    But keep in mind, strings are contained in matching pairs of quotes
    so
    var div = "document.getElementById("+id+")";

    that makes div a string of the object you want.
    Had you done div=eval(div); it would have worked, but your "ray of Light" was definately the correct way.

  4. #4
    charlesh's Avatar
    charlesh is offline Master Glow Jedi
    Join Date
    Aug 2006
    Location
    Atlanta, GA - better than you imagined it would be.
    Posts
    189

    Default

    Jmarc,

    Thanks for the reply. Yes, I didn't catch it because I was alerting the div var, but it came out as a string. Somehow, I thought I could include a string as part of a procedure... Wrong!

    I'll have to look into eval() a bit more, but you're right - the obvious thing to do here is to do it right the first time.

    What I'm finding is that in development, you really have to be fluent in 3 languages - php / SQL / javascript. Add in CSS notation and you've got a 4th, but CSS really makes things easier, not harder - anything not to have to go back to nested tables...

    I've started messing around with the YUI connection manager for the AJAX requests thanks to your suggestion. There are so many out there, it's hard to say which one, but it's all about the same.

    Chas

  5. #5
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    fluent in 3 languages ... and CSS ... you got it!

    The reason I settled on the yahoo offering is, many of the other ones (like scriptaculous) have very large footprints. YUI is more modular. Plus they offer compressed Libs and even have a JAVA compressor. Development seems to be ongoing, Docs are great, and support in the forums is excellent.

    When scriptaculous went to 300K for the lib and I started getting timeouts on my users I went to AJS. Not bad, but docs were scanty, few bugs, no support, and it was time to change yet again.

Similar Threads

  1. Javascript html timed popup
    By andychev in forum General Support
    Replies: 0
    Last Post: 11-08-2006, 06:03 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16