0

The idea is to get rid of double click in the input or drugging mouse to select all digits. Like 99% of User do these redundant actions. I'm one of them as I need to test stuff and it makes me crazy to not being able to have it selected right away.

Aura Component.

<lightning:input 
  aura:id="percentage"
  type="number" 
  name="percentInput" 
  disabled="{!v.percentInputDisabled}" 
  label=" %" 
  value="{!v.percent}" 
  formatter="percent-fixed" 
  step="1"
  onchange="{!c.handlePercentChange}"
  variant="label-inline"
  onfocus="{!c.handlePercentOnfocus}"
  tabindex="1"
/>

I found a lot of version of how to do that in the js controller. One of the version:

handlePercentOnfocus : function(component, event, helper) {
    let inputField = component.get("v.percent").getElement();
    inputField.setSelectionRange(0, inputField.value.length);
}

None of it works.

Maybe someone has working code. Please share.

EDIT1:

<lightning:input 
  aura:id="percentageAId"
  type="number" 
  name="percentInput" 
  disabled="{!v.percentInputDisabled}" 
  label=" %" 
  value="{!v.percent}" 
  formatter="percent-fixed" 
  step="0.01"
  onchange="{!c.handlePercentChange}"
  variant="label-inline"
  onfocus="{!c.handlPercentOnfocus}"
  tabindex="1"
/>



handleDepositPercentOnfocus : function(component, event, helper) {
    const inputField = component.find("percentageAId");
    console.debug("\n --- CreateXDialogController.js - handlePercentOnfocus - inputField: ", inputField);
    inputField.getElement().select();
}

The console outputs me a bunch of functions, but none of them are 'getElement()'.

addHandler: function value()​
​addValueProvider: function value() ​
blur: function value()
​checkValidity: function value()​
destroy: function value()​
focus: function value()​
get: function value(name)​
getEvent: function value()​
getGlobalId: function value()​
getLocalId: function value()​
isInstanceOf: function value() ​
isRendered: function value()​
isValid: function value()​
reportValidity: function value()​
set: function value()​
setCustomValidity: function value()​
showHelpMessageIfInvalid: function value()
​toString: function value()

1 Answer 1

1
handlePercentOnFocus : function(component, event, helper) {
 let percentage = component.find("percentageAId");
 const value = percentage.get("v.value");
 
 percentage.set("v.type", "text");
 
 percentage.set("v.selectionStart", 0);
 percentage.set("v.selectionEnd", value.length);
 
 percentage.set("v.type", "number");
}
2
  • 1
    Sorry, but this is a nice idea badly presented. Suggestions: 1. Remove anything suggesting you used AI. AI is not allowed on StackExchange. 2. Correct the spelling of the function (mind the capitals in "handlePercentOnFocus") and the component. 3. Do not expect to get a string (for the "value"), but rather convert to a string. 4. Write a small note on what your idea was, instead of only throwing code at the reader. This will make everything look much better. Commented Nov 11 at 17:00
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Nov 11 at 17:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .