SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    get ouput inside a div

    I'm unclear how to get this function to display "document.getElementById("txtHint").innerHTML=xmlhttp.responseText;" inside the div. Will you show me please?

    Script:
    Code:
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    	  var div = document.createElement('div');
          div.setAttribute('id', 'txtHint');
          div.setAttribute("style","background-color:red;");
          div.style.width = '300px';
          div.style.height = '100px';
          document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
           //var txt='hello world!';
          document.getElementsByTagName('body')[0].appendChild(div);
          document.getElementById('mydiv2').innerHTML=txt;
        }
      }

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of

    Quote Originally Posted by nichemtktg View Post
    Code:
          document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    try

    Code:
    div.innerHTML=xmlhttp.responseText;
    But it's not a good idea to have variable names the same as html element names. I would change the name of your div variable to something else.

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Though, how do you assemble it so it displays the response inside the div?

  4. #4
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If xmlhttp.responseText is not appearing inside the div whose id is txtHint, then there is a problem somewhere else in your code.

    If you haven't already done so, change the name of the variable currently called div. It might not be an issue in this case, but you shouldn't have variable names the same as html element names.

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK. I've worked on this some more and got it to work the way I need to work. How could I clean it up and what issues do you see? Please be a kind as possible this is my first javascript function.
    Code:
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    	  var div = document.createElement('div');
          div.setAttribute('id', 'link_container');
          div.setAttribute("style","background-color:white;");
          div.style.width = '300px';
          div.style.height = '100px';
          div.style.margin = '-15px 0px 0px 75px';
          var txt=document.innerHTML=xmlhttp.responseText;
          document.getElementsByTagName('body')[0].appendChild(div);
          document.getElementById('link_container').innerHTML=txt;
        }

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank-you very much for your help webdev1958.


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
  •