11

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...

Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
NumerousHats
  • 636
  • 5
  • 14
  • possible duplicate of [Find value of current line of a – Andy E Nov 17 '11 at 16:22
  • @AndyE There is a pretty substantial difference between \n and a line that user sees in a textarea. For example, this comment doesn't have a single newline but I am already on the third line of the textarea :) – Esailija Nov 17 '11 at 16:30
  • @Esailija: the OP wasn't specific enough for us to say, but it's worth flagging just in case that is what the he's looking for. That's why the comment says *"possible duplicate..."*. – Andy E Nov 17 '11 at 18:20
  • Sorry about the ambiguity. I mean "\n". And no, I don't believe my question is a duplicate of the "Find value of current line" question: I need something that will trigger an event and change something on the page in response to the user moving to a different line. – NumerousHats Nov 17 '11 at 21:11
  • I apologize for any stupidity in my question: I am a near-total newbie wrt Javascript, though I'm a pretty experienced programmer in other languages. In fact, I'm asking this mainly to see if this is at all feasible before I spend more time learning Javascript. – NumerousHats Nov 17 '11 at 21:17

1 Answers1

3

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

Community
  • 1
  • 1
DefyGravity
  • 5,681
  • 5
  • 32
  • 47
  • http://stackoverflow.com/q/1937120/176818 and here is an example of the jQuery fieldselection plugin http://laboratorium.0xab.cd/jquery/fieldselection/0.2.3-test/test.html – DefyGravity Nov 17 '11 at 16:37
  • See my comments above... So, it sounds like it should be feasible, and the way to do it is by keeping track of clicks and keypresses and then comparing the previous and current line and seeing if there has been a change? I should be able to figure it out, once I see the strategy. – NumerousHats Nov 17 '11 at 21:21
  • And yes, I have no objections to libraries. I will almost certainly be using jQuery for other aspects of the project (if I choose to do it as a webapp...) – NumerousHats Nov 17 '11 at 21:24
  • @dinkorlitz see the edits i made for what I think needs to be done. – DefyGravity Nov 18 '11 at 16:01
  • Brilliant! Thanks. Will keep your code around for when I get around to that part of the project. – NumerousHats Nov 23 '11 at 03:17
  • Minor comment now that I'm finally getting around to implementing this part of my webapp: for future reference, arrow keys don't trigger a "keypress" event on Chrome. You need to use "keydown" or "keyup". – NumerousHats Jan 07 '12 at 13:28