You are here

4 February 2011
You can use these examples to change img tags src attribute or any attribute of any tag.
The most simple solution is to change image by class or id, this works for first tag with that class:
Source code viewer
  1. <script type="text/javascript">
  2. $('.class').attr('src','image/dir/image-name.png');
  3. </script>
Programming Language: Javascript
This code replaces src attributes of all tags with the class name:
Source code viewer
  1. <script type="text/javascript">
  2. $('.class').each(function(){
  3. $(this).attr('src','image/dir/image-name.png');
  4. });
  5. </script>
Programming Language: Javascript
This example changes size of all images in a tag which class is declared:
Source code viewer
  1. <script type="text/javascript">
  2. $('.class a').each(function(){
  3. $(this).attr('style','width:150px;'+$(this).attr('style'));
  4. });
  5. </script>
Programming Language: Javascript