link1096 link1097 link1098 link1099 link1100 link1101 link1102 link1103 link1104 link1105 link1106 link1107 link1108 link1109 link1110 link1111 link1112 link1113 link1114 link1115 link1116 link1117 link1118 link1119 link1120 link1121 link1122 link1123 link1124 link1125 link1126 link1127 link1128 link1129 link1130 link1131 link1132 link1133 link1134 link1135 link1136 link1137 link1138 link1139 link1140 link1141 link1142 link1143 link1144 link1145 link1146 link1147 link1148 link1149 link1150 link1151 link1152 link1153 link1154 link1155 link1156 link1157 link1158 link1159 link1160 link1161 link1162 link1163 link1164 link1165 link1166 link1167 link1168 link1169 link1170 link1171 link1172 link1173 link1174 link1175 link1176 link1177 link1178 link1179 link1180 link1181 link1182 link1183 link1184 link1185 link1186 link1187 link1188 link1189 link1190 link1191 link1192 link1193 link1194 link1195 link1196 link1197 link1198 link1199 link1200 link1201 link1202 link1203 link1204 link1205 link1206 link1207 link1208 link1209 link1210 link1211 link1212 link1213 link1214 link1215 link1216 link1217 link1218 link1219 link1220 link1221 link1222 link1223 link1224 link1225 link1226 link1227 link1228 link1229 link1230 link1231 link1232

[Vue.js] Bootstrap-vue textarea.selectionStart is NaN or undefined

I’m trying to detect when the user is pressing the tab key, so I can insert spaces, instead of shifting the element focus.

The event handler is called correctly, but the value of both .selectionStart and .selectionEnd properties are either undefined or NaN. Is there something wrong with my code?

<b-form-textarea
id=”pythonCodeInput”
placeholder=”Write Python code here…”
v-model=”newQuestion.objects.code”
ref=”codeInput”
@keydown.native.tab=”keyDownInTextarea”>
</b-form-textarea>

Relevant code from keyDownInTextarea

let codeInput = this.$refs.codeInput;

// Add 4 spaces
let tabSize = 4;
let tabPosition = codeInput.selectionStart;
let textWithSpaces = codeInput.value.substring(0, tabPosition);
for (let i = 0; i < tabSize; i++) textWithSpaces += “ “;
textWithSpaces += codeInput.value.substring(tabPosition);

I expect tabPosition to have an value showing where the cursor is, but the actual output is NaN or undefined.

Solution :

When you attached a ref to <b-form-textarea>, what you get is the reference to an instance of the <b-form-textarea> component and not the textarea element which you are trying to access. Refer to
docs for more info.

To access the actual <textarea> element, you can do it by accessing the inner $refs of the codeInput ref since under the hood, the textarea element has a ref called input.

To put it simply, it would look like this.

// this is a reference to the `<b-form-textarea>` component instance.
let codeInput = this.$refs.codeInput;

// get the actual DOM element
const textArea = codeInput.$refs.input;

See working example

Solution 2:

I’m new to vue.js so sry if I got something wrong.

Here is what I got after some testing. I replaced @keydown.native.tab with v-on:keydown.tab and that seems to work without any problems.

new Vue({
el: ‘textarea’,
methods: {
test: (e) => {
console.log(e.target.selectionStart);
}
}
})
<script src=”https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<textarea
id=”textarea”
v-on:keydown.tab=”test” ></textarea>