Reputation: 242
How to add id attribute of an element in HTML as a variable which is declared above in javascript.
jQuery
var table_row = $('table').find('tr#'+pid);
var name = table_row.find('td:nth-child(1)').html();
table_row.find('td:nth-child(6)').html('<button type="button" id="what to write here" class ="save_db")>save</button> ');
I want to set id as name.
Thanks
Upvotes: 0
Views: 1515
Reputation: 15606
It can be done using simple string concatenation as follows:
var table_row = $('table').find('tr#'+pid);
var name = table_row.find('td:nth-child(1)').html();
table_row.find('td:nth-child(6)')
.html('<button type="button" id="'+name+'" class ="save_db">save</button> ');
PS: Also note that your markup contained a seemingly unnecessary closing brace
after class
attribute. I've removed it in my code.
Upvotes: 1
Reputation: 79
Have you tried this? Try using this:
<button type="button" id='"+name+"' class= "save_db">
Upvotes: 0
Reputation: 11154
Please try with the below code snippet. Let me know if i am not understand your question.
var id = "btn1";
var table_row = $('table').find('tr#'+pid);
var name = table_row.find('td:nth-child(1)').html();
table_row.find('td:nth-child(6)').html('<button type="button" id="'+id+'" class ="save_db")>save</button> ');
Upvotes: 0
Reputation: 2365
Please follow this code:
table_row.setAttribute("id", "id_you_like");
Upvotes: 0
Reputation: 17366
Concatenate name
variable to the appending string as ID
.
table_row.find('td:nth-child(6)').html('<button type="button" id="'+ name + '"
class ="save_db")>save</button>');
Upvotes: 0
Reputation: 14620
So you just want a variable inserted into the html string?
table_row.find('td:nth-child(6)').html`(
'<button type="button" id="' + name + '" class ="save_db">save</button> '
)`;
Upvotes: 0