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
1 Answer
Reset to default 3Don'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!