Skip to content

How to select elements based on partial id in jQuery

2011 November 16
by mark

I had a bunch of images on a webpage that had id’s that began with myimage and then had a number, like myimage1, myimage2, myimage3.  I wanted to select them all and change the image.  To do this you can use 1 line of the code:
$('img[id*=myimage]').attr("src", "/images/differentimg.jpg");
In this code you are telling jQuery to look for any img tags on your webpage that have the word myimage in their id and then change the src tag of the image to differentimg.jpg.

No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS