2

Looking at the Locker documentation they list selectionStart as being a valid attribute, but I can't find a way to access it.

I am trying to write an input masking utility, and everything works fine as long as the caret position is at the end of the input. If the user tries to edit in the middle, the caret gets moved to the end of the input.

If I were just working in normal javascript, I would simply call

var pos = document.activeElement.selectionStart;
...
document.activeElement.setSelectionRange(pos,pos);

But in Lightning Locker, activeElement is the lightning:input node and not the actual input, as such selectionStart is undefined.

From the onchange event, how can I get to the changed HTMLInputElement so I can call selectionStart and get the current caret position?

1 Answer 1

1

To answer your question directly, Salesforce would have to expose these attributes to you with additional code, etc. As such, there's no way to access the attributes unless they were exposed. You can write your own custom component, however, using SLDS.

Native HTML elements have limited Locker Service protection so you can access many of their normal attributes. You would need to do something like this anyways if you wanted special masks like ***-**-**** or (___) ___-____. The SecureElement reference is mostly meant to let developers know what attributes you can access from native HTML DOM elements.

3
  • Thanks sfdcfox. That was the answer that I expected to hear, but I was hoping you would have a better answer for me ;)
    – Tezyn
    Jan 20, 2019 at 3:44
  • @Tezyn Yes, Lightning is far more secure than Visualforce was, which is good, but also frustrating from a developer's point of view. I've already had to write a few custom components for SLDS-based things because of design choices on the standard components. Maybe this would be something worth posting an Idea for.
    – sfdcfox
    Jan 20, 2019 at 4:18
  • While I didn't get the answer that I liked, this is the correct one. I have posted an Idea success.salesforce.com/ideaView?id=0873A000000ECmoQAG for this functionality to be added.
    – Tezyn
    Jan 31, 2019 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.