Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set image src to another image jquery

Tags:

I have a div with some images, and when I clicked on those images I want another div to open with that Image that I clicked.

JS

$('.examples img').click(function() {     var loc = $(this).attr("src");     $('#image-zoom').attr("src",loc); }); 

HTML

<div class="container examples"  >     <div id="image-zoom">         <img class="img-thumbnail zoom" src="" alt="dental">     </div>     <div class="row">         <div class="col-sm-12">             <img id="zoom" class="img-thumbnail zoom" src="images/01.png" alt="dental">             <img class="img-thumbnail zoom" src="images/02.png" alt="dental">             <img class="img-thumbnail zoom" src="images/03.png" alt="dental">         </div>     </div>      <div class="row">         <div class="col-sm-12">             <img class="img-thumbnail zoom" src="images/04.png" alt="dental">             <img class="img-thumbnail zoom" src="images/05.png" alt="dental">             <img class="img-thumbnail zoom" src="images/06.png" alt="dental">         </div>     </div> </div> 

When I try to Hide the div its working, so I have error in syntax I think

like image 622
Hariss Avatar asked Apr 01 '14 19:04

Hariss


People also ask

How to set src for image in jQuery?

Answer: Use the jQuery attr() Method You can use the attr() method to change the image source (i.e. the src attribute of the <img> tag) in jQuery. The following example will change the image src when you clicks on the image.

How to add src value in jQuery?

You can use: $('#id'). attr('src', 'newImage. jpg');

How to add an image Using jQuery?

With jQuery, you can dynamically create a new image element and append it at the end of the DOM container using the . append() method.

How do I change my Onclick image in HTML?

The most you could do is to trigger a background image change when hovering the LI. If you want something to happen upon clicking an LI and then staying that way, then you'll need to use some JS. I would name the images starting with bw_ and clr_ and just use JS to swap between them.


2 Answers

Change the src of the image, not of the div:

$('#image-zoom img').attr("src",loc); 
like image 74
Callidior Avatar answered Oct 22 '22 15:10

Callidior


change line 3 to reference the image instead of its container:

$('#image-zoom img').attr("src",loc); 
like image 32
Sagi_Avinash_Varma Avatar answered Oct 22 '22 15:10

Sagi_Avinash_Varma