i_lalit
i_lalit

Reputation: 242

setting value of id attribute as variable in html

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

Answers (6)

Vikram Deshmukh
Vikram Deshmukh

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

Abdullah Obaidullah
Abdullah Obaidullah

Reputation: 79

Have you tried this? Try using this:

<button type="button" id='"+name+"' class= "save_db">

Upvotes: 0

Jayesh Goyani
Jayesh Goyani

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

Systematix Infotech
Systematix Infotech

Reputation: 2365

Please follow this code:

table_row.setAttribute("id", "id_you_like");

Upvotes: 0

Dhaval Marthak
Dhaval Marthak

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

David Barker
David Barker

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

Related Questions