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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Createelement - problems

    Hi everyone,

    I've recently started looking at AJAX and getting to grips with the DOM. Unfortunately, one thing I can't get my head around is the createelement.

    Here's my dilemma:

    I have a div, with some nested divs within it. For example:


    Code:
    <div id="container">
        <div id="content_1">content 1</div>
        <div id="content_2">content 2</div>
        <div id="content_3">content 3</div>
        <div id="content_4">content 4</div>
    </div>
    What I'm trying to do is make an AJAX request and with the response from the request, insert the response within the div id 'container'.

    So, for example, if the response from the AJAX request was:

    Code:
        <div id="content_5">content 5</div>
    I'd want that to appear within the 'container':

    Code:
    <div id="container">
        <div id="content_1">content 1</div>
        <div id="content_2">content 2</div>
        <div id="content_3">content 3</div>
        <div id="content_4">content 4</div>
        <div id="content_5">content 5</div>
    </div>
    It can appear anywhere within the 'container'. It doesn't have to be at the bottom.

    Can anyone help please?

    Thanks everyone
    Last edited by bwuk; 01-11-2007 at 04:34 PM. Reason: Problem has been resolved. Thanks

  • #2
    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
    If you do not intend to change later the DOM tree, you may use non-standard innerHTML method
    Code:
    ...
    var AJAXresponse='<div id="content_5">content 5</div>';
    var innerCont=document.getElementById('container').innerHTML;
    innerCont+=AJAXresponse;
    document.getElementById('container').innerHTML=innerCont;
    ...
    otherwise you should use DOM methods (createElement(), setAttributes()... ), which is a little bit intricate, as you either have change the response or to split the string response in order to get the needed values to be used in DOM methods (the tag name, the attributes, the attributes' values...)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts
    the DOM:

    Code:
            var someDIV= getElementByID("container");               
            var groupArray = someDIV.getElementsByTagName("DIV");
            someDIV.insertBefore(someDIV,groupArray[groupArray.length-1])
    would place your div on exact position as in your example.


  •  

    Posting Permissions

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