-1

The following script gets HTML from AJAX call - HTML displays fine by itself in browser - I use InnerHtml to put it within DIV, I get javascript error :

Uncaught TypeError: Cannot call method 'setData' of undefined

I have put the same exact HTML by hard-coding inside the template, it works fine - it just doesn't work when I insert it in DIV dynamically! any ideas?

<script> 
  $(document).ready(function() 
  { $('ul.art-vmenu li').click(function(e) 
     { 
       //Ajax call to get content:
       $.ajax(
       {
             type: "GET",
             url: "/create",
             data: "",
             success: function(msg)
             {
               //alert(msg);   
               document.getElementById("art-post-inner art-article").innerHtml = msg; 
               //$("#art-post-inner art-article").html(msg);  // jQuery call             
               window.clipboardData.setData("Text", msg);  // for debug in IE               
             }
       });

     });
  });    
</script>   
afshin
  • 1,488
  • 5
  • 18
  • 30
  • What do you mean by inserting in DIV dynamically ? – Dhruva Sagar Jun 16 '11 at 02:40
  • 1
    That error message is unrelated to `innerHTML`. The message expressly says it's about your `setData` call. Your quoted code won't produce that error, because it errors out earlier in the code (on the `document.getElementById("art-post-inner art-article").innerHtml = msg;` call). – T.J. Crowder Jun 16 '11 at 02:41

3 Answers3

3

first of all: it's innerHTML, not innerHtml

second: which browser are you using? clipboardData is only available in IE

check also: How do I copy to the clipboard in JavaScript?

Community
  • 1
  • 1
roberkules
  • 6,389
  • 1
  • 40
  • 51
  • Got it Working - Feel stupid it was innerHTML - somehow my brain saw it but it didn't register. Thanks – afshin Jun 16 '11 at 03:03
2

You're trying to reference innerHTML on an undefined value, because document.getElementById is returning undefined, because this code is asking for an invalid ID:

document.getElementById("art-post-inner art-article").innerHtml = msg; 
//            Spaces not allowed ------^     and      ^----- Should be innerHTML

(+1 to roberkules for pointing out the innerHTML thing, I read right past it.)

Rules for IDs vary depending on whether you're using HTML4 and below, HTML5 (which is much more permissive), or CSS (restrictive), but one thing they all have in common is that you cannot have a space in an ID:

My guess is that that isn't an ID, that you intend it to be a selector of some kind, but as you haven't shown any markup I can't help with indicating how you might fix it other than to say that if it's a selector, you don't want document.getElementById, you want to use jQuery's $ as you have elsewhere in your code.

Community
  • 1
  • 1
T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
1

You are getting "Uncaught TypeError: Cannot call method 'setData' of undefined" error because window.clipboardData is undefined. innerHtml is also wrong.

You should always use jquery html method to set the html to any dom element because jquery takes care of executing any script tags withing the markup which you are setting.

ShankarSangoli
  • 67,648
  • 11
  • 84
  • 121