I'm using a jquery url shortener (https://github.com/hayageek/jQuery-URL-shortener), and I'm trying to make it so when the jquery returns the shortened url it will replace the original url in the textarea. My problem is that the .replace() doesn't seem to be working here:
$("#button").click(function () {
regex = /(https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/g ;
var longUrlLink = $("#textarea").val().match(regex);
jQuery.urlShortener({
longUrl: longUrlLink,
success: function (shortUrl) {
$("#textarea").val().replace(longUrlLink, shortUrl);
}
});
});
The regex works fine, and shortUrl is is being returned fine, so the problem must lie with how I've written then last line, but I cannot seem to get it to work.
Any help is greatly appreciated.
1 Answer 1
You have to also set the new value:
$("#textarea").val($("#textarea").val().replace(longUrlLink, shortUrl));
answered May 1, 2014 at 20:44
t.animal
3,4281 gold badge28 silver badges26 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
shortUrlin your$('#textarea').val()it should automatically overwrite it --$("#textarea").val(shortUrl);$('#textarea').val(shortUrl);...