javascript - Replace function using jquery -
i have code here:
txtname.value = txtname.value.replace(/(\r?\n){2,}/, '\n').replace(/^\r?\n|\r?\n$/, ''); txturl.value = txturl.value.replace(/(\r?\n){2,}/, '\n').replace(/^\r?\n|\r?\n$/, ''); txtusage.value = txtusage.value.replace(/(\r?\n){2,}/, '\n').replace(/^\r?\n|\r?\n$/, '');
how put in jquery? i've tried many things , doesn't work.
txtname = $('#emotename'); txturl = $('#emoteurl'); txtusage = $('#emoteusage');
var $txtname = $('#emotename'), $txturl = $('#emoteurl'), $txtusage = $('#emoteusage'); $txtname.val($txtname.val().replace(/(\r?\n){2,}/, '\n').replace(/^\r?\n|\r?\n$/, '')); $txturl.val($txturl.val().replace(/(\r?\n){2,}/, '\n').replace(/^\r?\n|\r?\n$/, '')); $txtusage.val($txtusage.val().replace(/(\r?\n){2,}/, '\n').replace(/^\r?\n|\r?\n$/, ''));
mind take @ particular article regarding jquery performance! :)
this should work great too, instead of repeating code, create array jquery objects , apply same replaces each of them.
var $objs = [ $('#emotename'), $('#emoteurl'), $('#emoteusage') ]; $.each($objs, function() { var $this = $(this), val = $this.val(); $this.val(val.replace(/(\r?\n){2,}/, '\n').replace(/^\r?\n|\r?\n$/, '')); });
Comments
Post a Comment