Roi
Roi

Reputation: 29

jquery new id for img active

I'm using this code: http://jsfiddle.net/fGHmF/2/

now I want to add the word "size" to every IMG id. for instance:

    <img src='images/size1.png' alt='img1' id='size1' />
    <img src='images/size2.png' alt='img2' id='size2' />
    <img src='images/size3.png' alt='img3' id='size3' />
    <img src='images/size4.png' alt='img4' id='size4' />

what do i need to change in the java script?

Upvotes: 1

Views: 63

Answers (4)

Jitendra Pancholi
Jitendra Pancholi

Reputation: 7562

try below

$('img').each(function(i){
   $(this).attr('id', 'size' + $(this).attr('id')); 
});

see here

Upvotes: 0

DEMO

$('img').attr('id', function() {
   return 'size'+this.id;
});

Upvotes: 2

letiagoalves
letiagoalves

Reputation: 11302

$('img').attr('id', function() {
   return 'size' + this.id;
});

Upvotes: 6

DGS
DGS

Reputation: 6025

Try

$('img').each(function(){
    $(this).attr('id', 'size' + $(this).attr('id'))
})

Upvotes: 1

Related Questions