1

I am developing a small lightbox wordpress plugin in which if a user clicks an image the image should appear in the lightbox. How can i identify URL of the image in which the user clicks, so it will be useful in showing the image in lightbox. Is this can be done in jquery? Can anyone help?

2 Answers 2

4

You can do like this

$("img").click(function() {
    var img_path = $(this).attr('src');
})

EDIT

change

data: dataString,

to

data: 'img_path='+img_path,

and you will be able to access the image path in lightbox.php like this

$image_path = $_POST['image_path'];
Sign up to request clarification or add additional context in comments.

6 Comments

is there any way to pass this img_path value to php variable?
@raj: how about using ajax ?
$("img").click(function() { var img_path = $(this).attr('src'); $.ajax({ type:'POST', url: 'lightbox.php', dataType: 'HTML', data: dataString, success: function(data){ } }); });this is the ajax i tried but cant get it
Ya i tried but not working. Is there any changes to be made on success: function(data){ }
is it possible to append this img_path to img tag like this <img src"value of img_path here">
|
1

Getting the image url is easy.

$(function() {

  $('img').on('click', function( event ) {
    var source = this.src;    
    console.log( source );
  });

});

Comments

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.