i'm not that used to working with jquery so any help would be appreciated. I have written this function but cannot get it to work, can anyone tell me what is wrong.
$(function() {
$('#replace_button').onclick(function() {
$('#box_txt').val().replace(/\t/g, '[TAB]');
$('#box_txt').val().replace(/\n/g, '[BREAK]');
});
});
the html that accompanies it is
<textarea name='box_txt' id='box_txt' rows='6' cols='50'></textarea>
<br>
<input type='button' id='replace_button' value='Replace'>
I just want to replace all tabs with [TAB] and all linebreaks with [BREAK] when the button is pressed.
Many thanks.
val
returns a string, not a kind of pointer to the value. And replace
doesn't change the string you pass (strings are immutable in JavaScript) but returns a 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);
goes like this:
$(function() {
$('#replace_button').onclick(function() {
$('#box_txt').val($('#box_txt').val().replace(/\t/g, '[TAB]'));
$('#box_txt').val($('#box_txt').val().replace(/\n/g, '[BREAK]'));
});
});
you get the value manipulate it but never set it back in the textarea :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.