0

I am creating one div dynamically and want to add it inside another div.

           (js) var divtag = document.createElement("div");
                divtag.innerHTML = xmlhttp.responseText;//Ajax call working fine
                document.getElementById('con').appendChild(divtag);

html:

enter code here <div id="con"></div>

The the o/p I am getting from AJAX call is Ok, also i am able to view it in browser, but when I am doing a view source I am unable to see the div and its contents. It should come as :

enter code here <div id="con">
              <div>
           Contents added @ runtime
         </div>
          </div>

Can somebody suggest where I am going wrong?

3 Answers 3

2

You won't see the generated code in the browser by default, but you can use the Firefox extension Web Developer to view the code generated after executing js.

-- edit

also helpful extension for FF - FireBug.

Sign up to request clarification or add additional context in comments.

3 Comments

yeah, that I have tried...its working..but cant see it in view source..probaly thats not possible.
Web Developer context menu -> View Source -> View Generated Source or FireBug -> HTML Tab
try alerting the responseText and see if the the correct one, then check if there is no js error.
1

Use firebug - the best firefox web development addon in my opinion.

With firebug you can inspect the DOM:

alt text
(source: mozilla.org)

Comments

0

Do you have "con" div loaded on our DOM?

var divtag = document.createElement("div");
divtag.innerHTML = xmlhttp.responseText;//Ajax call working fine
// See before adding a child, does it exist?
alert(document.getElementById('con'));
document.getElementById('con').appendChild(divtag);

I don't know where you having this code. As it is important that the DOM should be loaded before checking/assigning anything to an element. To avoid such a mistake:

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}
addEvent(window, 'load', function() {
      var divtag = document.createElement("div");
      divtag.innerHTML = xmlhttp.responseText;//Ajax call working fine
      // See before adding a child, does it exist?
      alert(document.getElementById('con'));
      document.getElementById('con').appendChild(divtag);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.