puks1978
puks1978

Reputation: 3697

jQuery replace removes line breaks in IE

I have the code:

$("#txtDescription").blur(function(){
    $("#txtDescription").text($("#txtDescription").text().replace('@', ''));
})

which removes the @ symbol is someone copies it into a text area. The problem with this is that in IE (all versions) it also removes any line breaks which were added and adds all text on a single line with spaces. The other problem is that if I save the information to a DB table the line breaks are actually there so it only seems to be a visual thing. Not good if a user thinks their information is all on a single line.

Does anyone have any suggestions how to remove characters if a user pastes into a textarea?

Upvotes: 2

Views: 323

Answers (3)

scorpio1441
scorpio1441

Reputation: 3088

$("#txtDescription").blur(function(){
    var newval = $(this).val().replace('@','');
    $(this).val(newval);
})

Upvotes: 1

Reigel Gallarde
Reigel Gallarde

Reputation: 65264

$("#txtDescription").blur(function(){
    $(this).val(function(i,val){
        return val.replace('@', '')
    });
});

Upvotes: 1

Ken Browning
Ken Browning

Reputation: 29091

if #txtDescription is a <textarea> as your post suggests, then you want to use jQuery's val function instead of text.

Upvotes: 2

Related Questions