Reputation:
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.
Upvotes: 1
Views: 3730
Reputation: 60717
IDs must be unique. If you have more than twice the same ID, you're doing something wrong.
What you're looking for are class
es.
Here is a sample:
<input type="text" value="http://some.url" class="url">
And the jQuery code (way less complex than the others answers):
$( '.url' ).on( 'click', function() {
window.open( this.value );
} );
PS: thanks to this answer for the "similar to _blank" code.
Upvotes: 0
Reputation: 150020
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.
Upvotes: 0
Reputation: 144669
var href = $('#url').val();
$('#url').replaceWith('<a href="' + href + '">click text</a>')
Upvotes: 2