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
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

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

Screen Shot 2017-04-23 at 10.15.41 AM
»

How to Use jQuery to Create a Sticky Navigation on Scroll

Creating “sticky” elements is something that can often be ...

article image
»

How to Use jQuery’s Keystroke Methods

jQuery has a few built-in keystroke methods that are all triggered by ...

article image
»

How to Change iFrame Styling Using jQuery

If you’ve ever had the displeasure of working with iFrames, ...

article image
»

How to Use jQuery to Create Blinking Text

Blinking text is one of those cool features that you never actively ...

article image
»

Convert Images to Black and White using jQuery

Converting an image to black and white is no longer a manual event. ...

article image
»

How to Disable Right-Click Menu Using jQuery

There can be dozens of reasons why you may want to disable right ...

article image
»

How to Optimize jQuery Code for Better Performance

jQuery is the most popular client side library, in fact almost every ...

article image
»

Using Head.js to Load All Your JavaScript

In the past few years, client side frameworks/libraries have become ...