karz karz -4 years ago 164
Javascript Question

get textbox value inside gridview div tag in javascript

I have a textbox inside a div tag which is placed inside a

gridview
.
Below is the code :

<asp:TemplateField HeaderText="Color">
<ItemTemplate>

<div id="preview" style="width:100%; float:right">
<asp:TextBox ID="TB_color" runat="server" Width="50%" ReadOnly="true"></asp:TextBox>
</div>
<ajaxToolkit:ColorPickerExtender ID="ColorPickerExtender1" runat="server" targetcontrolid="TB_color" samplecontrolid="preview" PopupPosition ="Right" OnClientColorSelectionChanged="colorChanged" />
</ItemTemplate>
</asp:TemplateField>


How to read (or loop through) the TextBox
TB_color
value in javascript?

Answer Source

You can find ids that contains with TB_color to find all textboxes as asp.net generate ids that contains TB_color

for(i=0;i<document.forms[0].length;i++)
{   
    e=document.forms[0].elements[i];
    if (e.id.indexOf("TB_color") != -1 )
    {
       console.log(e.id);
    }
}   

If you can use jQuery then it will be very simple.

$('[id*=TB_color]').each(function(){
  console.log(this.id);
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download