1

I am a beginner for Javascript, read several post here but could not get my codes work. Hope you can explain the solution :)

So basically I have four set of images, but I need to replace all of the $_12.JPGto $_57.JPG in their URL so I get bigger size of pictures (only images that are assigned to class="ebay" / id="ebay"). I need this to be executed when the browser starts reading the page, no events or buttons involved.

The following is my source code:

<div>
<img class="ebay" id="ebay" u="image" src="http://i.ebayimg.com/00/s/NzgwWDc4MA==/z/neAAAOSwrklU28I8/$_12.JPG?set_id=880000500F">
<img u="thumb" src="http://i.ebayimg.com/00/s/NzgwWDc4MA==/z/neAAAOSwrklU28I8/$_12.JPG?set_id=880000500F">
</div>
<div>
<img class="ebay" id="ebay" u="image" src="http://i.ebayimg.com/00/s/NzgwWDc4MA==/z/r8cAAOSwNSxU28I9/$_12.JPG?set_id=880000500F">
<img u="thumb" src="http://i.ebayimg.com/00/s/NzgwWDc4MA==/z/r8cAAOSwNSxU28I9/$_12.JPG?set_id=880000500F">
</div>
<div>
<img class="ebay" id="ebay" u="image" src="http://i.ebayimg.com/00/s/NzgwWDc4MA==/z/HLgAAOSwPhdU28I-/$_12.JPG?set_id=880000500F">
<img u="thumb" src="http://i.ebayimg.com/00/s/NzgwWDc4MA==/z/HLgAAOSwPhdU28I-/$_12.JPG?set_id=880000500F">
</div>
<div>
<img class="ebay" id="ebay" u="image" src="http://i.ebayimg.com/00/s/NzgwWDc4MA==/z/6VkAAOSwPYZU28I-/$_12.JPG?set_id=880000500F">
<img u="thumb" src="http://i.ebayimg.com/00/s/NzgwWDc4MA==/z/6VkAAOSwPYZU28I-/$_12.JPG?set_id=880000500F">
</div>

I also like to loop the function so when I have more than 4 sets of pictures (maximum 15 sets) and the javascript can still replace the URL for me.

The following is what I had tried:

window.onload = function() {

    for ( var i = 0; i < currentLink.length; i++) {

    var currentLink=document.getElementByClassName('ebay');
    var newLink = currentLink.src.replace('$_12.JPG','$_57.JPG');
    currentLink.src = newLink;

    }
}
6
  • @Rakesh_Kumar No, it doesn't, it returns (when correctly written) a HTMLCollection which is an object. Commented Feb 16, 2015 at 18:13
  • Note that technically speaking, window.onload IS an event... actually you seem quite close to the solution... Commented Feb 16, 2015 at 18:15
  • @Teemu Isn't HTMLCollection is a collection of elements..so to say Array? Commented Feb 16, 2015 at 18:15
  • It's not an array, the said object doesn't have methods like pushor pop etc. Commented Feb 16, 2015 at 18:16
  • @Teemu Well if its an object.. how does it supports length property? Commented Feb 16, 2015 at 18:19

2 Answers 2

1

You have to realize that getElementsByClassName returns an HTMLCollection, not a single element. So you cannot simply change all the elements in the collection by accessing the .src property of the collection.

You need to iterate over the collection to do this:

var currentLinks = document.getElementsByClassName('ebay');

Array.prototype.forEach.call(currentLinks, function(currentLink) {
    var newLink = currentLink.src.replace('$_12.JPG','$_57.JPG');
    currentLink.src = newLink;
});

Also, you have a typo in your getElementByClassName (you're missing the s in Elements).

See MDN

Sign up to request clarification or add additional context in comments.

Comments

0

Misplacement of var currentLink=document.getElementByClassName('ebay');

Do,

window.onload = function() {

    var currentLink=document.getElementsByClassName('ebay');
    for ( var i = 0; i < currentLink.length; i++) { 
    var newLink = currentLink[i].src.replace('$_12.JPG','$_57.JPG');
    currentLink[i].src = newLink;
    }
}

1 Comment

Will give error....As you have done typo mistake getElementByClassName instead of getElementsByClassName..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.