jquery - Can we set the value of a control from variable -


is possible set value of control control id saved in variable. code looks like

var textmonth = $(this).parent().find('input[id$=txtmonths]').attr("id"); 

i'm trying this

textmonth.val("only numbers"); 

is possible?

the demo code given below:

the html code is:

<div id="pagecontent"> <h1>         collection master</h1>      <table cellpadding="1" cellspacing="2" style="padding-left:40px; font-size:medium; padding-right:20px"         width="100%" border="0">         <tr>                 <td colspan="2" style="width: 100%">                     <asp:gridview id="grdfees" runat="server" allowpaging="false" cssclass="grid" autogeneratecolumns="false"><columns>                              <asp:templatefield headertext="si no" headerstyle-horizontalalign="center"                                 itemstyle-horizontalalign="center" itemstyle-width="3%">                                 <itemtemplate>                                     <%# container.dataitemindex + 1%>                                 </itemtemplate>                             </asp:templatefield>                             <asp:boundfield headertext="sub category" datafield="sub_cat" itemstyle-cssclass="subcat" itemstyle-width="15%"                                 headerstyle-horizontalalign="center" itemstyle-horizontalalign="center" />                             <asp:boundfield headertext="fees paid (months,quarters)" datafield="paid_fees" itemstyle-width="15%"                                 headerstyle-horizontalalign="center" itemstyle-horizontalalign="center" />                                 <asp:boundfield headertext="pending fees" datafield="pend_fees" itemstyle-width="5%"                                 headerstyle-horizontalalign="center" itemstyle-horizontalalign="center" />                             <asp:templatefield headertext="current payment" headerstyle-horizontalalign="center" itemstyle-horizontalalign="center"                                 itemstyle-width="10%">                                 <itemtemplate>                                     <asp:textbox id="txtmonths" width="80%" runat="server"                                       cssclass="textbox"></asp:textbox>                                     <a href="#" class="openmodallink">                                         <img style="vertical-align: middle; border: none" width="9%" alt=""                                          src="../images/ico_map.gif"  id="imgmap" class="zoom"/></a>                                 </itemtemplate>                             </asp:templatefield>                         </columns>                     </asp:gridview>                 </td>             </tr> </table>     <div id="overlay" class="web_dialog_overlay">         </div>         <div id="dialog" class="web_dialog">             <table style="width: 100%; border: 0px;" cellpadding="3" cellspacing="0">                 <tr>                     <td class="web_dialog_title" style="height: 16px">                         month chooser</td>                         <td class="web_dialog_title align_right" style="height: 16px">                         <a href="#" id="btnclose">close</a>                     </td>                 </tr>                 <tr>                 <td colspan="2">                 <asp:gridview id="grdpopup" runat="server" tabindex="5">                         <columns>                         <asp:templatefield headertext="select" headerstyle-horizontalalign="center"                                 itemstyle-horizontalalign="center" itemstyle-width="2%">                                 <itemtemplate>                                     <asp:checkbox id="chkselect" runat="server" cssclass="checkbox"                                      width="15px" checked="false" />                                 </itemtemplate>                             </asp:templatefield>                             <asp:boundfield headertext="term" datafield="term" itemstyle-width="35%" headerstyle-horizontalalign="center" itemstyle-horizontalalign="left" itemstyle-cssclass="term" />                             <asp:boundfield headertext="fee amt." datafield="fee_amt" itemstyle-width="35%" headerstyle-horizontalalign="center" itemstyle-horizontalalign="center" itemstyle-cssclass="feeamt" />                          </columns>                     </asp:gridview>                 </td></tr>                 <tr>                     <td rowspan="2" colspan="2" style="text-align: center; vertical-align:middle">                         <input id="btnsubmit1" type="button" value="submit" class="button" />                     </td>                 </tr>                 </table>                 </div>  </div> 

the js code is

<script src="../jquery autocomplete/jquery-1.7.2.min.js" type="text/javascript">  </script> <script src="../jquery autocomplete/jquery.json-2.2.min.js" type="text/javascript">  </script>     <script type="text/javascript"> $(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);      }     </script> 

  1. first element id
  2. then use id jquery selector concating id selector

    var textmonth = $(this).parent().find('input[id$=txtmonths]').attr("id");  $('#'+ textmonth).val('your value'); 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -