I have a script that auto-replaces specific characters like double spaces
into single space
and straight quotes
into smart quotes
in the textarea
However, when I try to add onclick feature, it’s not working for me, it’s still executing it before the click of a button (the moment it detects the character).
Here’s what my it looks like right now:
JavaScript
x
42
42
1
function fixtext() {
2
let textarea = document.getElementById("textarea1");
3
textarea.select();
4
document.execCommand("fixtext");
5
}
6
7
window.addEventListener('DOMContentLoaded', function(e) {
8
var area = document.getElementById("textarea1");
9
10
var getCount = function (str, search) {
11
return str.split(search).length - 1;
12
};
13
14
var replace = function (search, replaceWith) {
15
if (typeof(search) == "object") {
16
area.value = area.value.replace(search, replaceWith);
17
return;
18
}
19
if (area.value.indexOf(search) >= 0) {
20
var start = area.selectionStart;
21
var end = area.selectionEnd;
22
var textBefore = area.value.substr(0, end);
23
var lengthDiff = (replaceWith.length - search.length) * getCount(textBefore, search);
24
area.value = area.value.replace(search, replaceWith);
25
area.selectionStart = start + lengthDiff;
26
area.selectionEnd = end + lengthDiff;
27
}
28
};
29
30
area.addEventListener("input", function (e) {
31
replace(" ,", ",");
32
replace(" ;", ";");
33
replace(" .", ".");
34
replace(" ", " ");
35
replace(" ", " ");
36
replace("--", "—");
37
replace(/(^|[-u2014s(["])'/g, "$1u2018");
38
replace(/'/g, "u2019");
39
replace(/(^|[-u2014/[(u2018s])"/g, "$1u201c");
40
replace(/"/g, "u201d");
41
});
42
});
JavaScript
1
2
1
<textarea id="textarea1" cols="40" rows="8"></textarea>
2
<button class="cbtn" title="Fix text" onclick="fixtext()">Fix text</button>
I really want it to work only after the user clicks the button, and not auto-replace before it. Please tell me how to fix this bug?
Advertisement
Answer
your function that replaces the content of the textarea is called in a input listener, so you should get it out in a separate function and call that function in the fixText function
JavaScript
1
45
45
1
function fixTextarea(textarea) {
2
textarea.value = textarea.value.replace(" ,", ",")
3
.replace(" ;", ";")
4
.replace(" .", ".")
5
.replace(" ", " ")
6
.replace(" ", " ")
7
.replace("--", "—")
8
.replace(/(^|[-u2014s(["])'/g, "$1u2018")
9
.replace(/'/g, "u2019")
10
.replace(/(^|[-u2014/[(u2018s])"/g, "$1u201c")
11
.replace(/"/g, "u201d")
12
};
13
14
15
16
function fixtext() {
17
let textarea = document.getElementById("textarea1");
18
textarea.select();
19
fixTextarea(textarea);
20
}
21
22
window.addEventListener('DOMContentLoaded', function (e) {
23
var area = document.getElementById("textarea1");
24
25
var getCount = function (str, search) {
26
return str.split(search).length - 1;
27
};
28
29
var replace = function (search, replaceWith) {
30
if (typeof (search) == "object") {
31
area.value = area.value.replace(search, replaceWith);
32
return;
33
}
34
if (area.value.indexOf(search) >= 0) {
35
var start = area.selectionStart;
36
var end = area.selectionEnd;
37
var textBefore = area.value.substr(0, end);
38
var lengthDiff = (replaceWith.length - search.length) * getCount(textBefore, search);
39
area.value = area.value.replace(search, replaceWith);
40
area.selectionStart = start + lengthDiff;
41
area.selectionEnd = end + lengthDiff;
42
}
43
};
44
45
});
JavaScript
1
2
1
<textarea id="textarea1" cols="40" rows="8"></textarea>
2
<button class="cbtn" title="Fix text" onclick="fixtext()">Fix text</button>