According to MDN, substr
is considered legacy code that may eventually become deprecated.
Hence I'm wondering whether substring
or slice
is better suited for inserting strings into other strings at specific indices?
Or is there another method that is actually intended for this very purpose?
What I'm doing is detecting user input via keydown
, which I then, in some cases, intercept and extrapolate to manually pass on the value.
input.onkeydown = function(e){
if (e.key === 'x'){
return false
}
this.value = // insert character at this.selectionEnd into this.value
return false
}
The reason I'm doing this is so that I can manage and restrict user input while guaranteeing the output is formatted in a particular way. In other words, the user will not have to format his/her input.