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

javascript - Inserting <select> button into HTML table using innerHTML - Stack Overflow

programmeradmin3浏览0评论

I am attempting to insert an input button into an HTML table using .innerHTML in the associated Javascript function.

Whenever I use the same JS, but replace the "select" with other HTML, it works perfectly fine. It is only < input type="button" /> and < button > that do not work.

The problem appears in at least IE, Firefox, and Chrome. Chrome returns an "Unexpected Identifier" error.

The abbreviated code (NOT the original):

function newrow() {
    table = document.getElementById("Table");
    row = table.insertRow(1);
    cell = row.insertCell(0);
    cell.innerHTML = "<input id="Button" type="button" value="ClickHere" />";
};

I have tried replacing the HTML with formatted text ("< b > Text < / b >") and links, and both display correctly.

I have also created the object outside of the script, and this does work, but seems unnecessary:

<input id="Button" type="button" value="ClickHere" />

<script type="text/javascript">

function newrow() {
    table = document.getElementById("Table");
    button = document.getElementById("Button");
    row = table.insertRow(1);
    cell = row.insertCell(0);
    cell.innerHTML = button;
};

This works, but is ungainly and plicates any "getindex"s I use.

Any help would be appreciated!

I am attempting to insert an input button into an HTML table using .innerHTML in the associated Javascript function.

Whenever I use the same JS, but replace the "select" with other HTML, it works perfectly fine. It is only < input type="button" /> and < button > that do not work.

The problem appears in at least IE, Firefox, and Chrome. Chrome returns an "Unexpected Identifier" error.

The abbreviated code (NOT the original):

function newrow() {
    table = document.getElementById("Table");
    row = table.insertRow(1);
    cell = row.insertCell(0);
    cell.innerHTML = "<input id="Button" type="button" value="ClickHere" />";
};

I have tried replacing the HTML with formatted text ("< b > Text < / b >") and links, and both display correctly.

I have also created the object outside of the script, and this does work, but seems unnecessary:

<input id="Button" type="button" value="ClickHere" />

<script type="text/javascript">

function newrow() {
    table = document.getElementById("Table");
    button = document.getElementById("Button");
    row = table.insertRow(1);
    cell = row.insertCell(0);
    cell.innerHTML = button;
};

This works, but is ungainly and plicates any "getindex"s I use.

Any help would be appreciated!

Share Improve this question edited Dec 10, 2017 at 10:07 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 27, 2012 at 20:07 LordGrtegLordGrteg 131 silver badge4 bronze badges 3
  • 1 A select with a type="button"? – epascarello Commented Sep 27, 2012 at 20:10
  • 2 Are you aware of the double quotes issue in "<select id="Button" type="button"...? – Michal Klouda Commented Sep 27, 2012 at 20:10
  • Not counting the major syntax error in your posted code cell.innerHTML = "<select id="Button" type="button" value="ClickHere" />";, there is no 'type="select"` for buttons. – Jeremy J Starcher Commented Sep 27, 2012 at 20:10
Add a ment  | 

2 Answers 2

Reset to default 5

An "Unexpected Identifier" error would be triggered by this line:

cell.innerHTML = "<input id="Button" type="button" value="ClickHere" />";

The problem is your quotes. The JavaScript interpreter is getting confused with the quotes used to represent strings in JavaScript and the quotes used inside HTML. You need to either escape the quotes within the string, or use single quotes for one of the purposes.

Single quotes would work:

cell.innerHTML = '<input id="Button" type="button" value="ClickHere" />';

Alternatively, escaping the quotes would work:

cell.innerHTML = "<input id=\"Button\" type=\"button\" value=\"ClickHere\" />";

I think you are looking for <input type="button" /> not select maybe? The select element is for dropdown menus.

发布评论

评论列表(0)

  1. 暂无评论