1
On Twitter or Facebook, when we type a hashtag the text background turns blue, as in the image below
I’m trying to do this on js using the .keyup()
in a div[contenteditable=true]
, I am doing with PHP passing the text by jQuery and returning the text if there is @and adding a class and a span. Works, but always returns the text the text cursor that is in div[contenteditable=true]
it always goes back to the beginning (left side) of the DIV and when I start the text (right side) it edits but back to the beginning the cursor (left side) again.
I know there might be another method but I’ve researched it and I haven’t found anything like it.
Code jQuery:
$(document).ready(function(){
$("#text_publica").keyup(function(){
var text = $("#text_publica").text();
var dataString = 'nome='+text;
$.ajax({
type: 'post',
url: 'pages/validate_nm.php',
data: dataString,
cache: false,
success: function(data){
$("#text_publica").html(data);
$("#text_publica").focus();
}
});
});
});
PHP code:
<?php
session_start();
include('main_bd.php');
include 'data.class.php';
if(isset($_POST['nome'])){
$txt = $_POST['nome'];
$txt = preg_replace('/\B@([\d\w_]+)/i', '<span class="tag">$0</span>', $txt);
echo $txt;
}
?>
It came to me an interest in doing this I had this idea to do with jQuery + PHP I’m not an expert in this area of js but I turn around when I need to. In this case I need your help, if you know how to do otherwise or make the text cursor always be in front of the last character right at the end of the text is already a start.
Good @klaiderklai, is giving error in Setcaretposition: setCaretPosition is not defined, and another when I start typing with (arroba)Labla with other texts nothing happens, when as normal ex: Hi (arroba)hey, the text erases all, it adds the <span class="tag"></span> only empty and gives the error of Setcaretposition, I tried to solve only not mt good in js as I mentioned in the topic. I use Google Chrome.
– Gleyson Abreu
@Hey, you need to copy the function
setCaretPosition
answer (I mentioned) in the other question. I will update.– Klaider
This setCaret was solved, as I said, when I start with (Arroba)Gleyson he does not put the span with the class tag, already when I start with: hi (Arroba)kk, goes more or less the first "defect" that it erases the whole text, according to "defect" is that you have to be very quick to say because the moment you put the @it already passes the span and the class with nothing, you have to press ex: @a simultaneously, is another and to put these functions within the $("#text_publica"). keyup(Function(){....});, Thanks in advance.
– Gleyson Abreu