0

I have 4 divs and on mouseover of each, I want the src of an img to change.
I have to get the images from PHP.
How can I use the array that I get in PHP over in my jQquery script?
And I know with just 4, I could just set up the array in javascript and put each image name, but that's not the functionality I'm looking for.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
div{
height:100px;
width:100px;
display:inline-block;
background-color:blue; 
}
#my_image{
height:100px;
width:100px;
display:inline-block;
}
</style>
<?php
$images = scandir("images", 1);
?>
<script>
$(document).ready(function(){
 $(div).mouseover(function(){
 $("#my_image").attr("src", /*my php array*/);
 });
});
</script>
</head>
<body>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <img id="my_image" />
</body>
</html>
Bogdan Burym
5,5022 gold badges30 silver badges47 bronze badges
asked Dec 19, 2012 at 16:58
2
  • 1
    It's not clear to me what you want the final result to be. A single image can only have one SRC. Commented Dec 19, 2012 at 17:02
  • I want the src to change to a different image based on which div the mouse is over. Commented Dec 19, 2012 at 17:06

1 Answer 1

6
var images = <?php echo json_encode($images); ?>;

Then you have an array called images that you can use in your JavaScript code. Using it at the position of your comment makes no sense though. You can't assign an array to the src of an image.

answered Dec 19, 2012 at 16:59
Sign up to request clarification or add additional context in comments.

1 Comment

right, it would be like this: $("#my_image").attr("src", images[0]);

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.