How can I get jquery/javascript to detect when -->
is typed in a textfield and replace it with →
?
Would something like the following be the best way of doing this:
$("input").on("keyup paste", function() {
var content = $(this).html(),
arrow = "→";
content = content.replace(regex, arrow);
$(this).html(content);
});
You can use String#replace
with regex to replace all occurrences of a string.
Note: input
elements have value
, use val()
to get the value of it.
$("input").on("keyup paste", function () {
$(this).val(function (i, val) {
return val.replace(/-->/g, '→');
});
});
$("input").on("keyup paste", function () {
$(this).val(function (i, val) {
return val.replace(/-->/g, '→');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />
I'll suggest to use change
event instead of keyup
.