Convert apple-tab-space to & nbsp;

is there a library that converts the apple-tab-space character to the   ?

If not, can you suggest an effective way to do this?

I have some problems with my code editor using contenteditble div (nevermind)

+4
source share
3 answers

Try listening to keydown and add &nbsp; in <input> val() :

 $(function () { $('textarea').keydown(function (e) { var code = e.keyCode || e.which; if (code == '9') { $(this).val($(this).val() + '&nbsb;') return false; } }); }); 

Plnk here: http://plnkr.co/edit/GF80PSJPLiBOCPJINDuv?p=preview

Key return false; is the key (no pun intended), since the input tab inside the input will in essence advance the user to the next input. It is worth noting that this is the expected behavior, so keep this in mind when introducing this feature to users.

Update: example without jQuery

It occurred to me that you did not mark this question with jQuery, so here is also a solution for vanilla JavaScript:

 window.addEventListener('load', function () { document.getElementsByTagName('textarea')[0].addEventListener('keydown', function (e) { var code = e.keyCode || e.which; if (code == '9') { this.value = this.value + '&nbsb;'; e.preventDefault(); } }); }, false); 

Plnk here: http://plnkr.co/edit/QJc3Lt2TQGBo8CQqtESO?p=preview

+3
source

Try this (replace "myDiv" with a selector for the actual targeting of the div):

 $('#myDiv').html(function() { return this.innerHTML.replace(/\t/g, '&nbsp;'); }); 

I extracted this piece of code from this answer: Replacing tab characters in JavaScript

+1
source

From your question, this sounds when in your browser or code editor on your Mac you want to reassign the key combination (command tab) to display & nbsp (skipped semicolon for display).

If this is really what you want to do, there is a great utility called keyremap4macbook that you can check here . I use it to reassign keys during remote desktop sessions, and it works great. You can only have mappings for specific applications, which is even better than the general key mapping utilities that are there.

+1
source

Source: https://habr.com/ru/post/1498537/


All Articles