Search code examples
jqueryimagesrc

Access img tag src using jQuery


Can I do this?

$("#myImage"+1).attr("src", "path/to/newImage.jpg");

Because the image is like an array so the id for the img tag is myImage1

<img src="path/to/oldImage.jpg" id="myImage1">

Solution

  • Yes, you could iterate round image elements like this:

    for (var i=0;i<=5;i++)
    {
     $("myImage" + i).attr("src", "path/to/newImage.jpg"); ?
    }