My actual requirement is, When I select some text in WordPress visual editor, and then my custom text is replace in place of that text. I can do this in HTML Editor of Wordpress, but not in Visual Editor, as it is an iframe.

I tried number of methods to fire a Javascript/jQuery function at onSelect event in TinyMCE Visual Editor in WordPress.

Only one thing worked but not fulfills my requirement, when I wrote a anchor tag after body part, and then the following script alerts the selected text.

jQuery('#something').click(function($) {
var selectedText = jQuery("#content_ifr").prop("contentWindow").getSelection().toString();
var linkText = "<a href='yourlink.html'>"+selectedText+"</a>";