Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to use variable value as id in javascript

iam assign textbox value to using json. using textbox id but id is an variable value . that variable value change in dynamic with use of loop.

What I have tried:

<asp:TextBox ID="title0" runat="server" >
<asp:TextBox ID="title1" runat="server" >


$.each(jsonData.d, function() {
var title = "title" + iCnt;
//var tittleid = title + iCnt;

$("[id$='title']").val(jsonData.d[iCnt].labeltext);
});
Posted
Updated 8-Aug-16 10:27am
v2
Comments
Karthik_Mahalingam 6-Aug-16 10:39am    
$("[id$=['titleid']")
try this
ZurdoDev 8-Aug-16 10:20am    
I do not understand you.

1 solution

lets say your JSON data looks like this:

JavaScript
[
   { textBoxId: "title0", value: "some value" },
   { textBoxId: "title1", value: "some other value" }
]


then your routine to set the values should be this:

JavaScript
$.each(jsonData.d, function(index, item){
    $('#' + item.textBoxId).val(item.value);
});


if the Id of the textbox coming back from the server isnt the client id of the textbox modify the .value js to

JavaScript
$('input[id*="' + item.textBoxId + '"]').val(item.value);
 
Share this answer
 
v2
Comments
Nethaji chennai 10-Aug-16 0:22am    
sorry Adam R Harris i dont understand you please explain
Adam R Harris 10-Aug-16 3:29am    
What part are you not understanding?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900