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

  • SEARCH
  • Home
  • 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 6834947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:10:26+00:00 2026-05-26T23:10:26+00:00

Is there a way to have a textarea fire an event if the user

  • 0

Is there a way to have a textarea fire an event if the user changes which line the caret is on, say, by clicking or using the up/down arrows? Or is this just not possible in Javascript? I’ve found ways to find/set the current position of the caret, but that’s not what I need…

  • 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-05-26T23:10:27+00:00Added an answer on May 26, 2026 at 11:10 pm

    Sounds like you need to register a couple events for your text area. off the top of my head, a click event, and a keypress event with multiple keycode values. Do you need to use pure javascript, or do you have some javascript libraries to utilize?

    Do you need help registering events? or do you need help finding the caret position during one of the events? (see andy’s link for that) or is the answer to both my questions "yes"?


    edit

    ok, from you’re comments you’re ok with jquery, and the fieldselection plugin prevents you from re-inventing the wheel.

    1. identify any keyboard keys, mouse clicks, ?copy paste? events that could move the caret in a text field. Navigate and select portions of text, wikipedia
    2. during the event, use the fieldselection plugin to get the new caret position.
    3. use the current caret position && font size && text box physical size && linebreak count to determine if you’ve switched to a new line.
    4. if you did switch lines, trigger a custom jQuery event to do the work you want.

    //jQuery 1.7

    $(document).ready(function(){
      var currentLine = -1;
      $("#textAreaID").on("keypress", function(evt){
       if(evt.which === 40 || ...){
    //down arrow key, enter key, page down key, all will move the caret to a newline
         $(this).trigger("newline");
       }else{
    //a ton of text in a fixed width textarea will move the cursor to a newline
         $(this).trigger("checkForNewline");
       }
      }).on("click checkForNewline", function(evt){
        var jqElem = $(this);
    //fieldSelection plugin call
        var range = jqElem.getSelection();
        if(range["row"] && range["row"] !== currentLine){
          currentLine = range["row"];
          jqElem.trigger("newline");
       }else{
          var handTypedNewlines = jqElem.val().split(/\r\n|\r|\n/);
          var userTypedRowcounts = Math.floor(wholeString.length / jqElem.cols) + (handTypedNewlines instanceof 'object')?handTypedNewlines.length:0;
          if(userTypedRowcounts != currentLine){
             currentLine = userTypedRowcounts;
             jqElem.trigger("newline");
          }
       }
      }).on("newline", function(evt){
       // do your work, caret is on a newline.
      });
    });
    

    referenced stack overflow.

    reference to get .split() regex

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way that a user can enter a URL in a textarea
I have a textarea which I'm using JavaScript to retrieve the value of and
(Flex 3) I have a TextArea component which needs to hold the user's clipboard
Is there a way to limit the characters per line in a textarea? For
I have a textarea setup with CKEditor (a WYSIWIG) in which the user is
Is there a way to have setup/teardown code automagically run before/after each test? Something
Is there a way to have a template specialization based on a range of
Is there a way to have ASP classic cdonts email, have an file attached
Is there a way to have hanging operators when indenting with tabs in Vim?
Is there a way to have rails print out a number with commas in

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.