Babby
Babby

Reputation: 54

Replacing special character code by respective special character automatically

$('#emailsubmit').click(function(){
    var data = CKEDITOR.instances.message.getData();
    data=data.replace(/\"/g,'"');
    data=data.replace(/ /g,' ');
    data=data.replace(/©/g,'©');
    $('#message_text').val(data);
});

This is the jquery function for replacing the special character code by special character.And I set it manually and I want set automatically ,how this could be done ?

Upvotes: 0

Views: 71

Answers (2)

Tomas Aschan
Tomas Aschan

Reputation: 60564

What you want to do looks a lot like html decoding. A much better way of accomplishing the same thing - with support for any HTML entities - is the approach shown here:

// Get the encoded string from the editor
var data = CKEDITOR.instances.message.getData();

// Decode it - this is where the magic happens =)
var decoded = $("<div/>").html(data).text();

// Set the textbox value
$('#message_text').val(decoded);

As Vaibs_Cool suggested in his answer, you can wrap this in a function, and then set that function as an event handler for whatever event you want to trigger on.

Upvotes: 1

Vaibs_Cool
Vaibs_Cool

Reputation: 6156

function replace_word(){  
    var data = CKEDITOR.instances.message.getData();
    data=data.replace(/\"/g,'&quot;');
    data=data.replace(/&nbsp;/g,' ');
    data=data.replace(/&copy;/g,'&#169;');
    $('#message_text').val(data);
}

And use this function on keypress on any input you are using.

Upvotes: 0

Related Questions