X

LATEST #JQUERY NEWS

Show Image Preview for External Images Using jQuery


You must have seen on many websites that as soon as you enter any image URL, the image preview is displayed on the screen. This is a very good idea to let your user know exactly which image they are referring to. This can be easily done via jQuery. In this post, let’s find out how to show image preview for the input URL using jQuery.

To display the image preview on screen, along with the input text box, we also need to have image tag. So your HTML should look like:

<div>
Image Url: <input type="text" id="txtURL"/>
<br />
<img id="imgPreview" />
</div>

Here is the jQuery code which will display the image preview as soon as the focus goes out of the input text box. The idea is very simple. You just copy the entered URL and assign the same to image element as the source and image will be loaded.

$(document).ready(function(){
  $('#txtURL').change(function () {
    $('#imgPreview').attr('src', $('#txtURL').val())
  }) 
});

So now, when you run your application and enter any valid image URL to input box and as soon as the focus goes out, the image URL will be assigned to image tag and it will be displayed.

Recently Published

article image
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of checkbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...

article image
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

Making a user friendly website can be quite a challenging task. There ...