Question

Is there a way that I can change a word to a link when keyup? For example, I want to convert all

programming

into

 <a title="best programming help"
         href="https://www.stackoverflow.com/">programming</a>

After you typed "programming" and another space. How can this be done?


Supplement: I'm typing this into a text area. (I'm using http://imperavi.com/redactor/, it's a plugin actually.)

I can have HTML in text area because I'm referring to the output of the text area. And technically, I'm using a wysiwyg :)

Thank you so much for your help :)

Was it helpful?

Solution

If you are OK with the link being written out in plain text into the same element that you are typing into, this might do the trick for you.

http://jsfiddle.net/paulinfrancis/XFA4e/

var $thetext = $('#the-text');
var typedText = '';
var replaceText = '<a title="best programming help" href="https://www.stackoverflow.com/">programming</a>';
var programmingRegex = /programming$/gi;

$thetext.on('keyup', function(data){   
    typedText = $thetext.val();

    var matches = typedText.match(programmingRegex); 
    if(matches){
        typedText = typedText.replace(programmingRegex, replaceText);
        $thetext.val(typedText);
    }
});

OTHER TIPS

Looking at their API they have this callback:

changeCallback: function(html)
    {
        console.log(html);
    }

which is fired everytime the content of the textarea changes.

You could search for the words you want in html and modify the words you find into links.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top