Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8897105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:15:01+00:00 2026-06-15T00:15:01+00:00

I have a problem to execute a javascript function on a aspx site that

  • 0

I have a problem to execute a javascript function on a aspx site that should insert a String at the beginning of the TextBox (MultiLine) Content and sets the Cursor in the middle of the inserted string.
It just don’t get called at all on my side.

ASPX Code:

    <head runat="server">
    <title>Unbenannte Seite</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function setCaretPosition(elemId, caretPos) {
            var elem = document.getElementById(elemId);

            if(elem != null) {
                if(elem.createTextRange) {
                    var range = elem.createTextRange();
                    range.move('character', caretPos);
                    range.select();
                }
                else {
                    if(elem.selectionStart) {
                        elem.focus();
                        elem.setSelectionRange(caretPos, caretPos);
                    }
                    else
                        elem.focus();
                }
            }
        }

        $("#<%=Button1.ClientID %>").click(function(e){
            e.preventDefault();
            var d = new Date();
            var curr_date = d.getDate();
            var curr_month = d.getMonth() + 1;
            var curr_year = d.getFullYear();
            var curr_hour = d.getHours();
            var curr_minute = d.getMinutes();
            var name = "Name";
            var wert = curr_date + "." + curr_month + "." + curr_year + " / " + curr_hour + ":" + curr_minute + " / " + name + "\n" + "----------------------------------------" + "\n";
            var len = wert.length();
            wert = wert + "\n" + "----------------------------------------" + "\n" + "\n";
            var input = $("#<%=tbVZusatz.ClientID %>");
            input.val(wert + input.val());
            input.val(input.val());
            setCaretPosition("<%=tbVZusatz.ClientID %>",len);
        });

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="tbVZusatz" runat="server" TextMode="MultiLine" Height="500px" Width="300px"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Stempel" />
    </div>
    </form>
</body>

How the inserted String should look like:

13.11.2012 / 13:08 / LightMonk
----------------------------------------
(Position of the Cursor here!)
---------------------------------------- 

Sources I used for this:

http://jsfiddle.net/VP9zT/1/

Set keyboard caret position in html textbox

Corrected Javascript:

<script type="text/javascript">
    $(document).ready(function(){
        $("#<%=Button1.ClientID %>").click(function(e){
            e.preventDefault();
            var d = new Date();
            var curr_date = d.getDate();
            var curr_month = d.getMonth() + 1;
            var curr_year = d.getFullYear();
            var curr_hour = d.getHours();
            var curr_minute = d.getMinutes();
            var name = "Name";
            var wert = curr_date + "." + curr_month + "." + curr_year + " / " + curr_hour + ":" + curr_minute + " / " + name + "\n" + "----------------------------------------" + "\n";
            var len = wert.length;
            wert = wert + "\n" + "----------------------------------------" + "\n" + "\n";
            var input = $("#<%=tbVZusatz.ClientID %>");
            input.val(wert + input.val());
            input.val(input.val());
            setCaretPosition("<%=tbVZusatz.ClientID %>",len);
        });
    });

    function setCaretPosition(elemId, caretPos) {
        var elem = document.getElementById(elemId);

        if(elem != null) {
            if(elem.createTextRange) {
                var range = elem.createTextRange();
                range.move('character', caretPos);
                range.select();
            }
            else {
                if(elem.selectionStart) {
                    elem.focus();
                    elem.setSelectionRange(caretPos, caretPos);
                }
                else
                    elem.focus();
            }
        }
    }


</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:15:02+00:00Added an answer on June 15, 2026 at 12:15 am

    First, your code should look like this, with handler registration on document.ready() (I removed the asp.net markup)
    http://jsfiddle.net/rMmwq/

    Please also note the

     var len = wert.length;
    

    instead of

     var len = wert.length();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with JavaScript magic. When I execute this code: var page
I have problem with this function. I will explain - I must execute function
I have a javascript function that has been driving me nuts. This is the
Possible Duplicate: Javascript infamous Loop problem? I have the following: function test(a,b,c){ console.log(a+b+c); }
I have a problem when trying to execute this update statement (below) using C#
I have a problem with executing large number of queries. I need to execute
I have a weird problem. When I execute a query the data is not
I have a large project that executes without problem when not debugging (even when
I have an onclick function which performs several tasks. In another JavaScript function I
How I can execute a JavaScript function/piece of code in a specific context which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.