I am new to JQuery and I have a simple script that is not working and I can’t figure why. I want to change a image by using the attr(). I have tried puting the script in the head section and it didn’t work. I have placed both the images in the same root file but still it doesn’t work. What am I doing wrong?
<!doctype html>
<html lang="en">
<head>
<title>Testing JQuery</title>
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<img src="20220801_021439_mfnr.jpg"
alt="my swollen feet"
heigth="500px" width="300px"
id="#myFeet">
<script type="text/javascript">
$(document).ready(function(){
$("#myPic").attr("src","IMG-20201105-WA0000.jpg");
});
</script>
</body>
I have also tried other JQuery instructions using attr() , those instruction too dont work but everything else does.
3
Answers
Your jQuery selectors
#myPic
andid="#myFeet"
don’t match. Also you don’t put a#
in front of theid
in theid
attribute. The#
is only used incss
andjs
.Should just be
ID must be same and id must be defined as id="myPic" not as id="#myPic" and id is wrong.
You could use the .attr() function to set attributes on given DOM element:
to change the image source
In your case