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

I am using Jquery to add rows dynamically. The problem is I am not able to assign "name" attr

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745148029a4613736.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信