holder.js
I want to dynamically add a placeholder image to my page.
Inserting it like so doesn't work:
$('<li>',{class:'file-item'})
.append($('<img>',{'data-src':'holder.js/150x150'}))
.append($('<span>',{class:'file-name'}).text(file.name))
.appendTo('#file-list');
Because the holder script has already ran and isn't searching for new elements.
We can, however, run it again manually:
Holder.run()
But then it will scan all the elements that are already added.
So...is there way I can get holder.js to create and give me back a DOM element so I can add it manually without re-running the whole thing?
holder.js
I want to dynamically add a placeholder image to my page.
Inserting it like so doesn't work:
$('<li>',{class:'file-item'})
.append($('<img>',{'data-src':'holder.js/150x150'}))
.append($('<span>',{class:'file-name'}).text(file.name))
.appendTo('#file-list');
Because the holder script has already ran and isn't searching for new elements.
We can, however, run it again manually:
Holder.run()
But then it will scan all the elements that are already added.
So...is there way I can get holder.js to create and give me back a DOM element so I can add it manually without re-running the whole thing?
Share Improve this question edited Feb 16, 2013 at 21:25 mpen asked Feb 16, 2013 at 21:20 mpenmpen 284k281 gold badges892 silver badges1.3k bronze badges 6- Holder is a jQuery plugin? Something else? – the system Commented Feb 16, 2013 at 21:23
-
Seems like there is an
add_image
method that could possibly handle this – Explosion Pills Commented Feb 16, 2013 at 21:23 -
1
...beware of
{class:'file-name'}
. It'll break in some older browsers. UseclassName
or"class"
. – the system Commented Feb 16, 2013 at 21:24 - @thesystem: holder.js is a standalone library for generating placeholder images. It's used in Twitter Bootstrap. Added link to question. – mpen Commented Feb 16, 2013 at 21:25
-
@thesystem: Excellent point about the
class
property. I did run into that before (in IE I think) and it confused the heck out of me. Gave a very misleading error. – mpen Commented Feb 16, 2013 at 21:26
1 Answer
Reset to default 8Pass a Node
as the images
property to Holder.run
and you'll be able to run Holder on any individual image. Holder itself doesn't create a DOM element, it just changes the src
value.
Code:
var image = $("<img>").attr({
"data-src": "holder.js/300x200"
})
Holder.run({
images: image[0]
});
image.appendTo("body");
Live example here: http://jsfiddle/imsky/p3DMa/