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

javascript - How to add name attribute to <td> tag while adding rows dynamically - Stack Overflow

programmeradmin0浏览0评论

I am using Jquery to add rows dynamically. The problem is I am not able to assign "name" attribute to the table cell. I know how to add "name" attribute when we are creating input type="text" element in the cells, but here I do not want to see the values in the text boxes.

My problem is I am not able to assign "name" attribute to my table cells so that I can access their values by

document.getElementByNames("anyName")[0].value

My code:

function addRow() {
var carName = "fiat";
var Engine = "Petrol";
$("#datble").append("<tr><td>"+carName+"</td><td>"+Engine+"</td></tr>")
}

I am using Jquery to add rows dynamically. The problem is I am not able to assign "name" attribute to the table cell. I know how to add "name" attribute when we are creating input type="text" element in the cells, but here I do not want to see the values in the text boxes.

My problem is I am not able to assign "name" attribute to my table cells so that I can access their values by

document.getElementByNames("anyName")[0].value

My code:

function addRow() {
var carName = "fiat";
var Engine = "Petrol";
$("#datble").append("<tr><td>"+carName+"</td><td>"+Engine+"</td></tr>")
}
Share Improve this question edited Dec 15, 2015 at 18:32 Wai Ha Lee 8,83699 gold badges59 silver badges95 bronze badges asked Dec 15, 2015 at 18:10 IshanIshan 622 silver badges8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Don't use name for td, it is not a valid attribute for td, use class instead

var i=0;
function addRow() {
  var carName = "fiat";
  var Engine = "Petrol";
  $("#datble").append("<tr><td class='anyClass"+i+"0'>"+carName+"</td><td class='anyClass"+i+"1'>"+Engine+"</td></tr>");
  i++;
}

Now access the values using class selector like $(".anyClass00").html();

EDIT: Used i to make it more dynamic in nature!

发布评论

评论列表(0)

  1. 暂无评论