25276

using selectionStart on programmatically-created inputs

Question:

Is it possible to set and get .selectionStart and .selectionEnd on a <textarea> that I create programmatically?

<h2>Example</h2> var input = document.createElement("textarea"); input.selectionStart; // 0 - just to confirm the property exists input.selectionStart = 2; // returns 2 input.selectionStart; // returns 0, it should be 2

The above works fine in the textarea actually exists in the HTML of the page, but not when done as above (in Chrome at least).

The reason I want to do this is because I am testing a directive in AngularJS that manipulates a textarea and as part of my test I want to specify the position of the caret.

If not possible, is there a reason? And is there an alternative to setting the caret in a unit test?

Answer1:

<a href="http://jsfiddle.net/Zn3tT/" rel="nofollow">Fiddle</a>

var input = document.createElement("textarea"); // Add the manually created element to the document body document.body.appendChild(input); // Just added to confirm visually input.textContent = "Something clever here"; input.selectionStart = 2; // returns 2 input.selectionEnd = input.textLength - 2;

Recommend

  • Nginx rewrite equivalent to Apache RewriteRule that converts URL params into QueryString key/value p
  • Can't connect Entity Framework to local SQL Server Express
  • How to extract a number from a string [duplicate]
  • RavenDB indexing errors
  • Pre-populated SQLite Database not reading properly in Android Studio
  • What command do i need to pass in SabreCommandLLSRQ to get current price of PNR?
  • Cannot save model when using ember render helper
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • Trying to get the char code of ENTER key
  • Using Sax parsing to edit and write XML in VB6
  • Is playing sound in Javascript performance heavy?
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Debug.DrawLine not showing in the GameView
  • Groovy: Unexpected token “:”
  • Chrome doesn't support silverlight anymore? How to solve this?
  • Yii2: Config params vs. const/define
  • How to create a file in java without a extension
  • Database structure design with variable amounts of fields
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • How do I fake an specific browser client when using Java's Net library?
  • Sony Xperia Z Tablet not found by adb
  • How to get a value (ex: baseURL) in every Karate feature?
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Fill an image in a square container while keeping aspect ratio
  • MySQL WHERE-condition in procedure ignored
  • ILMerge & Keep Assembly Name
  • Javascript convert timezone issue
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • Large data - storage and query
  • Web-crawler for facebook in python
  • How can I estimate amount of memory left with calling System.gc()?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • WOWZA + RTMP + HTML5 Playback?
  • A cron job substitute?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Hits per day in Google Big Query
  • Checking variable from a different class in C#
  • Reading document lines to the user (python)