javascript - jquery function to replace textarea text not working -


i'm not used working jquery appreciated. have written function cannot work, can tell me wrong.

$(function() {    $('#replace_button').onclick(function() {      $('#box_txt').val().replace(/\t/g, '[tab]');      $('#box_txt').val().replace(/\n/g, '[break]');    }); }); 

the html accompanies is

<textarea name='box_txt' id='box_txt' rows='6' cols='50'></textarea>   <br>   <input type='button' id='replace_button' value='replace'> 

i want replace tabs [tab] , linebreaks [break] when button pressed.

many thanks.

val returns string, not kind of pointer value. , replace doesn't change string pass (strings immutable in javascript) returns new one.

you may use

var field = $('#box_txt'), s = field.val(); s = s.replace(/\t/g, '[tab]').replace(/\n/g, '[break]'); field.val(s); 

demonstration


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -