convert text to link by id jquery

advertisements

I want to convert plaintext to link for an input I have. The HTML code is:

<input type="text" value="http://www.test.com" id="url">

So each time this input with this id is on the page converted to clickable link to open in new page.


Something like this:

var $el = $("#url"),
    url = $el.val();

$el.replaceWith( $("<a />").attr({"href":url,"target":"_blank"}).html(url) );

Place that in a document ready handler or in a script block at the end of the body.

You didn't say what you wanted the text of the link to be so I've just repeated the url as shown in this working demo: http://jsfiddle.net/khJx2/1/

Specifying the "target" attribute as "_blank" will open the link in a new page (or tab, depending on the browser). Remove that to have it open in place of the current page.