locked
How To Add Todays Date In Textbox jquery or javascript without postback RRS feed

  • Question

  • User-807418713 posted

    Hello

    I have one textbox1 and one dropdownlist1 in dropdownlist1 i have two values one is Dep1, Dep2

    If user select Dep1, then in textbox1 he will enter for example 997  then on change i want to the same textbox like this

    A997/03-Jul-19

    If user select Dep2, then in textbox1 he will enter for example 884  then on change i want to the same textbox like this

    B884/03-Jul-19

    Here 03-Jul-19 is todays date how to do this using javascript without postback

    Thank You

    Wednesday, July 3, 2019 4:07 AM

Answers

  • User288213138 posted

    Hi Gopi.MCA,

    I put the DropDownlist and TextBox into the Updatepanel, and it still works.

    Please check your code and post your aspx code if there are any problems.

    The code:

    <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
        <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
        <script>
            $(document).ready(function () {
                $("#TextBox1").blur(function () {
                    if ($('#DropDownList1 option:selected').text()=="Dep1") {
                        var text = "A" + $("#TextBox1").val() + "/";                                   
                    }
                    else if($('#DropDownList1 option:selected').text()=="Dep2") {
                        var text = "B" + $("#TextBox1").val() + "/";
                    }
                    var date = $.datepicker.formatDate("dd-M-y", new Date()); 
                    var total = text + date;
                    $("#TextBox1").val(total);
    
                    
                })
            })
        </script>
    <div> 
                <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server">
                            <asp:ListItem>Dep1</asp:ListItem>
                            <asp:ListItem>Dep2</asp:ListItem>
                        </asp:DropDownList>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </ContentTemplate>
                </asp:UpdatePanel>          
            </div>
    

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 5, 2019 7:41 AM

All replies

  • User288213138 posted

    Hi Gopi.MCA,

    You can use the jQuery-ui plugin datepicker to get the current date.

    Then judge the content to be displayed according to the value of DrowDownList.

    The code:

      <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
        <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
        <script>
            $(document).ready(function () {
                $("#TextBox1").blur(function () {
                    if ($('#DropDownList1 option:selected').text()=="Dep1") {
                        var text = "A" + $("#TextBox1").val() + "/";                                   
                    }
                    else if($('#DropDownList1 option:selected').text()=="Dep2") {
                        var text = "B" + $("#TextBox1").val() + "/";
                    }
                    var date = $.datepicker.formatDate("dd-M-y", new Date()); 
                    var total = text + date;
                    $("#TextBox1").val(total);
    
                    
                })
            })
        </script>
    <div>           
                <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem>Dep1</asp:ListItem>
                    <asp:ListItem>Dep2</asp:ListItem>
                </asp:DropDownList>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </div>
    

    The result:

    Best regards,

    Sam

    Thursday, July 4, 2019 7:52 AM
  • User-807418713 posted
    Hi

    Thanks For Your Reply

    This code not working In Updatepanel why
    Thursday, July 4, 2019 5:23 PM
  • User288213138 posted

    Hi Gopi.MCA,

    I put the DropDownlist and TextBox into the Updatepanel, and it still works.

    Please check your code and post your aspx code if there are any problems.

    The code:

    <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
        <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
        <script>
            $(document).ready(function () {
                $("#TextBox1").blur(function () {
                    if ($('#DropDownList1 option:selected').text()=="Dep1") {
                        var text = "A" + $("#TextBox1").val() + "/";                                   
                    }
                    else if($('#DropDownList1 option:selected').text()=="Dep2") {
                        var text = "B" + $("#TextBox1").val() + "/";
                    }
                    var date = $.datepicker.formatDate("dd-M-y", new Date()); 
                    var total = text + date;
                    $("#TextBox1").val(total);
    
                    
                })
            })
        </script>
    <div> 
                <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server">
                            <asp:ListItem>Dep1</asp:ListItem>
                            <asp:ListItem>Dep2</asp:ListItem>
                        </asp:DropDownList>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </ContentTemplate>
                </asp:UpdatePanel>          
            </div>
    

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 5, 2019 7:41 AM