tags:

views:

544

answers:

2

i have a json object returned from ajax and when i alert it, it is displayed correctly and i try to add those into a unordered list and add that to a place holder div, but throws the above error..

function handleResponse() {
  if(httpa.readyState == 4){
  var response = httpa.responseText;
    //alert(response);
    if(response!='empty')
    {
      //alert(response);
      eval("prod="+response);
      var len = prod.length;
      var st = "<ul>";
      for(var cnt=0;cnt<len;cnt++)
      {
        st = st + "<li onclick='set("+prod[cnt].id+")'>"+prod[cnt].name+"</li>";  
      }      
      st = st + "</ul>";
      }
      var tt = document.getElementById('holder1');
      tt.appendChild(st); // i even tried **tt.appendChild(eval(st));**
      tt.style.display = 'block';  
    }

}
+1  A: 
tt.innerHTML += st;

As st is a string, not a DOM element.

Fabien Ménager
that doesn't work. I can see the code when i view the source... But what may be the problem. the rendered code is like this.<tr><td><input name="pname1" onkeyup="showlist(this.value);" type="text"><div style="display: block; z-index: 100;" id="holder1"><ul><li onclick="set(1)">Pepsodent 100g</li><li onclick="set(2)">Pepsodent 40g</li><li onclick="set(3)">Pepsodent brush</li></ul></div></td><td><input name="price1" class="rjust" type="text"></td><td><input name="quan1" class="rjust" onkeyup="settotal(1)" type="text"></td><td><input name="tprice1" class="rjust" type="text"></td></tr>
kvijayhari
What's wrong with the rendered code. That looks like what you wanted.
Justin Johnson
Don't use "innerHTML +="!It's inefficient and resets the state of the DOM that can't be serialized (e.g. attached event listeners), since it first serializes the content of an element, appends the string to the serialization, parses the new string, and replaces current children of an element with the new content.
Nickolay
Nothing is wrong with the rendered code but there is nothing to see..I can't see a div with list items... It is visible only when i view source..
kvijayhari
+1  A: 

A few comments:

  • eval("prod="+response); - don't do that. It's creates a global 'prod' variable, can execute arbitrary code, prevents the JS engine from speeding up your code, and generally is considered a bad coding practice.
    • Use a JSON parser instead (either from json.org or helpers from your favorite library).
  • tt.appendChild(st); // i even tried **tt.appendChild(eval(st));** - appendChild takes a DOM node; st is a string and eval(st) evaluates st assuming it contains JavaScript code (so running it on XML will result in a syntax error, unless you're using E4X, which still wouldn't create an object suitable for use with appendChild).
    • You should either parse the HTML code you've built (via innerHTML, createDocumentFragment, or -- again -- using a helper from your favorite JS library)
  • Finally, if you do this a lot, consider using templates instead.
Nickolay
now i had cleared that using a innerHTML Property and now it works.. And as far as the display i added an empty <a> after <li> and now my items are working but little bit in an awkward manner.. Its displayed below the table-cell .. If i want a item to be displayed as an separate element from the table/parent , how could i do.. By the way i'm looking ar using templates.. so have to move my code a bit
kvijayhari
it's not really clear what you mean. Could you post a small test page demonstrating the problem somewhere like http://jsbin.com/ or http://jsfiddle.net/ ?
Nickolay
http://jsbin.com/odale3 can u see that
kvijayhari
@Holicreature: I meant a somewhat working page - here, I fixed it up for you: http://jsbin.com/odale3/3/edit (you missed a <body> tag and had an extra </script> in the JavaScript section). Now how do I see the problem? You have to replace XMLHttpRequest with code feeding a constant string to the code that builds the DOM.
Nickolay