0

I have an image on a HTML page that's set to be blurry in a CSS file. What I want to do is create JavaScript code so when the image is clicked, the image is no longer blurry. Ideally, I would like an onClick event handler to change the CSS file and make the image unblurry.

Alternatively, I have been achieving this by creating a second image which is already filtered as blurry and when onClick occurs, the source of image is changed to the unblurry image. However, this is tedious and would like a simpler way to do it.

Additionally, I would like for the JavaScript to work for numerous HTML pages with images on different pages, instead of including it on each page for that particular image.

Sample code of previous method:

 <html>
 <head>
 <link rel="stylesheet" type="text/css" href="style.css">
 <script src="javascript.js"></script>
 </head>
 <body>

 <div id="image">
    <img id="img" src="imgblur.jpg" height=300 width=300>
 </div>   
 </body>
 </html>

JavaScript:

window.onload = init;
function init() {
var image = document.getElementById("img");
image.onclick = showAnswer;
}

function showAnswer() {
var image = document.getElementById("img");
image.src = "img.jpg";
}

Code for wanted approach:

 <html>
 <head>
 <link rel="stylesheet" type="text/css" href="style.css">
 <script src="javascript.js"></script>
 </head>
 <body>

 <div id="image">
    <img id="img" src="img.jpg" height=300 width=300>
 </div>   
 </body>
 </html>

CSS:

img {
filter: blur(5px);
}

JavaScript:

When image is clicked, the CSS is changed and blur filter is removed.

Thanks in advance :)

1
  • 1
    Classic downvote. I'm fairly sure there is a gang of High level programmer on SO that just laugh at noobs question and give them downvotes. OP has a problem, can you help instead of bury him ? Commented Jun 28, 2016 at 17:48

2 Answers 2

1

Try something like this

Jquery

$('#img').click(function(){
   $(this).css({'filter':'blur(0px)'})
});
Sign up to request clarification or add additional context in comments.

1 Comment

im sorry can you please elborate @ObinnaNwakwue
0

You can add an onClick method on your image, like this:

...
<img id="img" src="img.jpg" height=300 width=300 onClick="myFunction()">
...

This mean, when your image is clicked, the function myFunction() is called and you do what you need in there.

3 Comments

Have you got 2 img files? One blurry and other not blurry? If so, you can change the img source in the function you're calling in the onClick method.
Would rather access the DOM than using HTML element attributes if you get me?
Yeah I have two images, one blurry and one not blurry. What I was doing before was changing the image source (see above code), but I have many HTML pages with lots of images so creating and editing all them is tedious so would prefer to use CSS to alter image and then use JavaScript to remove filter when clicked.

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.