title: Can we set the value of a control from variable
body: <p>Is it possible to set the value of a control whose control id is saved in a variable. The code looks like</p>
  
  <pre><code>var textmonth = $(this).parent().find('input[id$=txtmonths]').attr("id");
  </code></pre>
  
  <p>I'm trying to do something like this</p>
  
  <pre><code>textmonth.val("Only numbers");
  </code></pre>
  
  <p>Is it  really possible?</p>
  
  <p>The demo code is given below:</p>
  
  <p>The html code is:</p>
  
  <pre><code>&lt;div id="pagecontent"&gt;
  &lt;h1&gt;
          Collection Master&lt;/h1&gt;
  
      &lt;table cellpadding="1" cellspacing="2" style="padding-left:40px; font-size:medium; padding-right:20px"
          width="100%" border="0"&gt;
          &lt;tr&gt;
                  &lt;td colspan="2" style="width: 100%"&gt;
                      &lt;asp:GridView ID="grdFees" runat="server" AllowPaging="false" CssClass="Grid" AutoGenerateColumns="false"&gt;&lt;Columns&gt;
  
                              &lt;asp:TemplateField HeaderText="SI NO" HeaderStyle-HorizontalAlign="center"
                                  ItemStyle-HorizontalAlign="center" ItemStyle-Width="3%"&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;%# Container.DataItemIndex + 1%&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:BoundField HeaderText="Sub Category" DataField="SUB_CAT" ItemStyle-CssClass="subcat" ItemStyle-Width="15%"
                                  HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" /&gt;
                              &lt;asp:BoundField HeaderText="Fees Paid (Months,Quarters)" DataField="PAID_FEES" ItemStyle-Width="15%"
                                  HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" /&gt;
                                  &lt;asp:BoundField HeaderText="Pending Fees" DataField="PEND_FEES" ItemStyle-Width="5%"
                                  HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" /&gt;
                              &lt;asp:TemplateField HeaderText="Current Payment" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center"
                                  ItemStyle-Width="10%"&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:TextBox ID="txtmonths" Width="80%" runat="server" 
                                       CssClass="Textbox"&gt;&lt;/asp:TextBox&gt;
                                      &lt;a href="#" class="openModalLink"&gt;
                                          &lt;img style="vertical-align: middle; border: none" width="9%" alt="" 
                                          src="../Images/ico_map.gif"  id="imgmap" class="zoom"/&gt;&lt;/a&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                          &lt;/Columns&gt;
                      &lt;/asp:GridView&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
  &lt;/table&gt;
      &lt;div id="overlay" class="web_dialog_overlay"&gt;
          &lt;/div&gt;
          &lt;div id="dialog" class="web_dialog"&gt;
              &lt;table style="width: 100%; border: 0px;" cellpadding="3" cellspacing="0"&gt;
                  &lt;tr&gt;
                      &lt;td class="web_dialog_title" style="height: 16px"&gt;
                          Month Chooser&lt;/td&gt;
                          &lt;td class="web_dialog_title align_right" style="height: 16px"&gt;
                          &lt;a href="#" id="btnClose"&gt;Close&lt;/a&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                  &lt;td colspan="2"&gt;
                  &lt;asp:GridView ID="grdpopup" runat="server" TabIndex="5"&gt;
                          &lt;Columns&gt;
                          &lt;asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="center"
                                  ItemStyle-HorizontalAlign="center" ItemStyle-Width="2%"&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:CheckBox ID="chkselect" runat="server" CssClass="checkbox" 
                                      Width="15px" Checked="false" /&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:BoundField HeaderText="Term" DataField="TERM" ItemStyle-Width="35%" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="Left" ItemStyle-CssClass="term" /&gt;
                              &lt;asp:BoundField HeaderText="Fee Amt." DataField="FEE_AMT" ItemStyle-Width="35%" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" ItemStyle-CssClass="feeamt" /&gt;
  
                          &lt;/Columns&gt;
                      &lt;/asp:GridView&gt;
                  &lt;/td&gt;&lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td rowspan="2" colspan="2" style="text-align: center; vertical-align:middle"&gt;
                          &lt;input id="btnSubmit1" type="button" value="Submit" class="button" /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;/table&gt;
                  &lt;/div&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>The JS code is</p>
  
  <pre><code>&lt;script src="../Jquery Autocomplete/jquery-1.7.2.min.js" type="text/javascript"&gt; 
  &lt;/script&gt;
  &lt;script src="../Jquery Autocomplete/jquery.json-2.2.min.js" type="text/javascript"&gt; 
  &lt;/script&gt;
      &lt;script type="text/javascript"&gt;
  $(document).ready(function() 
      {
          var textmonth;
          var textamount;
  
          $(".openModalLink").click(function()
          {
              textmonth = $(this).parent().find('input[id$=txtmonths]').attr("id");
              textamount = $(this).parent().parent().find('input[id$=txtAmount]').attr("id");
              ShowDialog();
          });
          $("#btnClose").click(function(e) 
          {
              textmonth = null;
              textamount = null;
              HideDialog();
          });
          $('#btnSubmit1').click(function(e) 
          {
              var month = null;
              var amount = 0;
              $(':checkbox:checked').each(function(i) 
              {
  
                  var amt=$(this).closest("tr").find(".feeamt").text();
                  if(amt != '')
                  {
                      amount = amount + parseInt(amt,10);
                      if(month == null)
                      {
                          month = $(this).closest("tr").find(".term").text();
                      }
                      else
                      {
                          month = month + ',' + $(this).closest("tr").find(".term").text();
                      }
                  }
              });
              $("#"+textamount).val(amount);
              $("#"+textmonth).val(month);
              HideDialog();
          });
      });
      function ShowDialog(modal)
      {
          $("#overlay").show();
          $("#dialog").fadeIn(300);
          if (modal) {
              $("#overlay").unbind("click");
          }
      }
      function HideDialog() 
      {
          $("#overlay").hide();
          $("#dialog").fadeOut(300);
  
      }
      &lt;/script&gt;
  </code></pre>