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
marseilles84
4262 gold badges9 silver badges21 bronze badges
-
1It's not clear to me what you want the final result to be. A single image can only have one SRC.Blazemonger– Blazemonger2012年12月19日 17:02:51 +00:00Commented Dec 19, 2012 at 17:02
-
I want the src to change to a different image based on which div the mouse is over.marseilles84– marseilles842012年12月19日 17:06:15 +00:00Commented Dec 19, 2012 at 17:06
1 Answer 1
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
ThiefMaster
320k85 gold badges608 silver badges648 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
marseilles84
right, it would be like this: $("#my_image").attr("src", images[0]);
default