最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 'String contains an invalid character' when using document.createElement() - Stack Overflow

programmeradmin0浏览0评论
var NewRow = document.createElement("<tr><td align='left' valign='top'  width='9%;'  ><img width='32px' height='32px' src='images/" + ProfilePic + "'  /></td><td align='left' valign='Top' ><span class='MsgSpan'>" + Msg + "</span></td><td align='right' align='left' valign='top' style='color:Gray;' >" + Date + "</td></tr>");

I am getting an error:

InvalidCharacterError: String contains an invalid character

How can I fix this?

var NewRow = document.createElement("<tr><td align='left' valign='top'  width='9%;'  ><img width='32px' height='32px' src='images/" + ProfilePic + "'  /></td><td align='left' valign='Top' ><span class='MsgSpan'>" + Msg + "</span></td><td align='right' align='left' valign='top' style='color:Gray;' >" + Date + "</td></tr>");

I am getting an error:

InvalidCharacterError: String contains an invalid character

How can I fix this?

Share Improve this question edited Jan 10, 2018 at 15:51 mikemaccana 123k110 gold badges427 silver badges531 bronze badges asked Aug 23, 2013 at 6:38 SoraSora 2,55119 gold badges77 silver badges150 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 20

The string you pass to document.createElement is the type of the element, e.g. tr.

If you really want to assemble your HTML as a big string, I suppose you could write:

var newRow = document.createElement('tr');
newRow.innerHTML = "<td align='left' valign='top'  width='9%;'  ><img width='32px' height='32px' src='images/" + ProfilePic + "'  /></td><td align='left' valign='Top' ><span class='MsgSpan'>" + Msg + "</span></td><td align='right' align='left' valign='top' style='color:Gray;' >" + Date + "</td>";

but it's probably cleaner/faster/safer to use DOM manipulation for the whole thing.

Check extra space after class name

In my case I had an extra space when adding a css class to a div.

I had this

menuLinksDiv.classList.remove('show-element ');

Removed the space after show-element string and no error

menuLinksDiv.classList.remove('show-element');

I have one similar answer below: Here, I have defined the elements in HTML and not created them separately, so that it will be easy to change and debug.

var ProfilePic = "abl.jpg";
var Msg="Hi";
var Date = "June 10, 2010";
function doit() {
   var NewRow ="<tr><td align='left' valign='top'  width='9px'  ><img width='32px' height='32px' src='images/" + ProfilePic + "'  /></td><td align='left' valign='Top' ><span class='MsgSpan'>" + Msg + "</span></td><td align='right' align='left' valign='top' style='color:Gray;' >" + Date + "</td></tr>";
   var element = document.getElementById("tbl_body");
   element.innerHTML +=NewRow;
}

The problem with the previous code was createElement where you have to create row, then td then text and then append.

Check this out:

Creating a new DOM element from an HTML string using built-in DOM methods or Prototype

You can not pass HTML string to createElement method, instead use innerHTML property as shown in the above link.

发布评论

评论列表(0)

  1. 暂无评论