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
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts

    Diff between static & dynamic element

    Code:
    b.appendChild(newDiv);
    This produces thing that I can see in firebug, but not in source of page.

    Having problems with some jquery code that works on static(written in page source file) div(that div is the target where result appears), but nothing happens on dynamic created div (doh it is exactly the same).


    Actual code:
    Code:
     if (document.getElementById("de_map") == null)
              {    
                targetDivId = 'de_map';
                var divList = document.getElementsByTagName('div');
                var newDiv = document.createElement('div');
                newDiv.setAttribute('id',targetDivId,true);
                newDiv.className = 'mapdiv';
                divList[0].parentNode.insertBefore(newDiv, divList[0].nextSibling);
              }
            
    
            
            $(function(){
      $('#de-map').vectorMap ....

    Comments ...
    Last edited by BubikolRamios; 12-27-2013 at 08:37 AM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,622
    Thanks
    6
    Thanked 1,001 Times in 974 Posts
    Quote Originally Posted by BubikolRamios View Post
    Having problems with some jquery code that works on static(written in page source file) div(that div is the target where result appears), but nothing happens on dynamic created div (doh it is exactly the same).
    Most likely this is because the element doesn’t exist yet when the script tries to access it. Or, from the looks at your snippet of code, and without knowing any other context, it looks like you try to append the new div before the browser has parsed all code, so it doesn’t even know where to append it yet.
    Try it by moving the new div script into the document ready function:
    Code:
    $(function(){
     if (document.getElementById("de_map") == null)
              {    
                targetDivId = 'de_map';
                var divList = document.getElementsByTagName('div');
                var newDiv = document.createElement('div');
                newDiv.setAttribute('id',targetDivId,true);
                newDiv.className = 'mapdiv';
                divList[0].parentNode.insertBefore(newDiv, divList[0].nextSibling);
              }
      $('#de-map').vectorMap(…);
    But that said, if you already use jQuery, why don’t you also use it to create and append the element? Less code to write:
    Code:
    $(function(){
    if(!$('#de_map').length) {
      $('<div>', {
        id: 'de_map',
        'class': 'mapdiv'
      }).appendTo(???); // use either appendTo(), before(), or after(); you can read all about those in the jQuery documentation
    }
    $('#de-map').vectorMap(…);

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    Quote Originally Posted by BubikolRamios View Post
    This produces thing that I can see in firebug, but not in source of page.
    of course. the source page is only the input file from which the browser creates the initial page state. after reading that in it’s not used any more.
    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


  •  

    Posting Permissions

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