Friday, July 29, 2022

JQuery get value and set value by ASP.NET textbox ID with ternary operator

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">


function test() {

         

         var recipient = $("#<%=txtTheoryHours.ClientID%>").val();

         

         var theoryHours = $('#<%=txtTheoryHours.ClientID %>').val() == '' ? 0 : $('#<%=txtTheoryHours.ClientID %>').val();

         var practicalHours = $('#<%=txtPracticalHours.ClientID %>').val() == '' ? 0 : $('#<%=txtPracticalHours.ClientID %>').val();

//to get the value

         var ojtHours = $('#<%=txtOJTHours.ClientID %>').val() == '' ? 0 : $('#<%=txtOJTHours.ClientID %>').val();

         var genericNOSHours = $('#<%=txtGenericNOSHours.ClientID %>').val() == '' ? 0 : $('#<%=txtGenericNOSHours.ClientID %>').val();

         var nsqfReimburseCourseDuration = parseInt(theoryHours) + parseInt(practicalHours) + parseInt(ojtHours) + parseInt(genericNOSHours);

//to set the value

         $('#<%=txt_NSQFReimburseCourseDuration.ClientID %>').val(nsqfReimburseCourseDuration);

         $('#<%=txt_NSQFcourseDuration.ClientID %>').val(parseInt(theoryHours) + parseInt(practicalHours));

        

     }


<script>

<asp:TextBox ID="txtTheoryHours" runat="server" MaxLength="4"  placeholder="Enter theory hours"

                    CssClass="form-control" onchange="test()"  OnTextChanged="txtTheoryHours_textChanged" ></asp:TextBox>




or we can use clientidmode to static and directly use txt id

 <asp:TextBox ID="txtPIPD" runat="server" ClientIDMode="Static"/>

javascript

 $(function () {

            $("#txtPIPD").datepicker({

                changeMonth: true,

                changeYear: true

            });

        });

No comments:

Post a Comment