i’ve got the following problem. Downloaded one script, and it’s loading all images from div’s body. I want to add one more image here, which won’t be processed by the script.
It’s like that in code:
<div id="myGallery0" class="spacegallery">
<img src=ADDITIONAL.jpg alt="" atr1="1" />
<img src=images/bw1.jpg alt="" atr1="1" />
<img src=images/bw2.jpg alt="" atr1="2" />
<img src=images/bw3.jpg alt="" atr1="3" />
</div>
So my question is, how to select in jquery all of these images EXCEPT the “ADDITIONAL.jpg” one?
My second question is how to select it in natural JavaScript by using one of the GetElementsBy functions.
Raw JavaScript
The raw JavaScript version uses
document.getElementByIdandElement#getElementsByTagNameand then skips the first one:Starting the loop with
index = 1skips the first one (which would beindex = 0).Or you can get really tricky, but I’d test it thoroughly on your target browsers:
That works because according to the specification,
Array.prototype.slicemust allow the object it’s working with to be something other than an array, as long as it supports the operations listed in the specification for theslicemethod. And theNodeListreturned bygetElementsByTagNamedoes, so we can callslicepassing it theNodeListas thethisvalue and telling it we want a slice starting with index 1. But note that the spec also quite clearly says “…Whether theslicefunction can be applied successfully to a host object is implementation-dependent.” andNodeListis a host-provided object, hence the recommendation to test thoroughly in your target environments.jQuery
In jQuery, there are lots of ways to do it. My favorite would probably be the one Rui pointed to, using
slice: