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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AJAX and document object?

    If I have a div in a document containing ordered lists (ul,li etc) and I use AJAX interaction with the server to modify the content of the div without page refresh, am I right in assuming that I cannot inspect or manipulate this new content using document.getElementById and similar methods of working with the document object? Or is there some way to do this?

    G

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    if you add a new element into the innerHTML of the div, you can manipulate as if it was originally part of the DOM. So yes, if you have an id attribute in an element, once it's in the innerHTML, you can use getElementById for it. That's the reason innerHTML is so slow compared to innerText, but FF doesn't have innerText. innerHTML actually processes the text going in.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Beagle,

    Ah, thanks. When I look at the DOM inspector, I do see the added elements there, yet if I try to get those elements in an array and alert with the length property of the array, the javascript console tells me that that element has no properties. Perhaps they have not loaded fully at the point I do the alert? Or what? Do I need some kind of special loader?

    G

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmmm, I think the answer is that the element that ajax is adding to must already be defined in the document.

    G

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    gorilla1: Speculating is all nice and so, but it's not likely to give you the answer. Can you show us the code you're using to insert it into the document, and the code you're trying to access it with, please?
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I am affraid that, as long as you have inserted your AJAX new elements via innerHTML method and not via DOM methods, your elements will no longer take part of your DOM tree structure.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    If you want to update the DOM tree then you have to use the DOM methods to insert the content (as Kor said) as innerHTML does not update the DOM tree.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks... Yes, I am using innerthml to post the dynamic content from the server. I left the code at work, but it is pretty straightforward... An unordered list structured with javascript and containing hyperlinks. Onclick a request is generated to code on the server that returns additional list items to be added via innerhtml.

    G

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How would you use DOM methods to insert the content?

    G


  •  

    Posting Permissions

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