0

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.

4
  • 1
    maybe I haven't read into it much, but if you just place shortUrl in your $('#textarea').val() it should automatically overwrite it -- $("#textarea").val(shortUrl); Commented May 1, 2014 at 20:42
  • @ntgCleaner is correct. If all you're trying to do is overwrite the value then you just need $('#textarea').val(shortUrl);... Commented May 1, 2014 at 20:43
  • The textarea val will usually have more characters in it than just the url, and I don't want to replace all of that, so this wouldn't work Commented May 1, 2014 at 20:44
  • you need to set the value after shortening it. Commented May 1, 2014 at 20:46

1 Answer 1

1

You have to also set the new value:

$("#textarea").val($("#textarea").val().replace(longUrlLink, shortUrl));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.