The Memorial Day weekend is coming to a close – I’ve been away from the computer for almost 48 hours (gasp!) and I want to get another jQuery tutorial up and online quickly.
I’m dying to do the write up for “Edit In Place the Jquery Way” and “Multiple File Upload Magic” but the code for both is a little more in depth and it just feels like I should tackle a few of the easier tutorials in the beginning.
So here goes –
Ever since making the switch away from table layouts for my websites in favor of all CSS (it must be two and a half years or more now) I’ve been consuming all the information I can find on the subject.
Way back in May of 2004 (ancient history) A List Apart had a great tutorial on creating drop shadows (Onion Skinned Drop Shadows) for any image, regardless of size.
The comments for the article are no longer available, but some of them echoed the sentiment from the editor’s note at the beginning of the tutorial.
Editor’s Note: The techniques demonstrated in this tutorial are not for everyone. The design method works its magic by nesting divs that have no semantic or structural value. If that bothers you (and there are good reasons why it might), this is not the tutorial for you.
(Emphasis added by Jack)
Some CSS techniques require “extraneous” markup because currently only one background image can be assigned per element.
For example:
Here is the html code used in the A List Apart tutorial:
<div class="wrap1"> <div class="wrap2"> <div class="wrap3"> <img src="object.gif" alt="The object casting a shadow" /> </div>< </div> </div>
All of those divs serve as "hooks" for background images that make up the drop shadow.
Woudn't it be nice if we could trim down our source code to:
<img src="object.gif" class="dropshadow" alt="The object casting a shadow" />
Which leads us to...
I'm going to show you how easy it is to remove extraneous markup from your html source code using jQuery. Using this method will keep your code cleaner and (more importantly) will make it future layout changes much easier.
Here's how jQuery attacks the problem.
$(document).ready(function(){
$("img.dropshadow")
.wrap("<div class='wrap1'><div class='wrap2'>" +
"<div class='wrap3'></div></div></div>");
});
And then the images would be styled like so:
<img src="object.gif" class="dropshadow" alt="The object casting a shadow" />
$(document).ready() is jQuery's version of window.onload()
$("img.dropshadow") tells jQuery to find all images with the class name "dropshadow". If you wanted to use an id instead, you could do something like $("img#dropshadow")
wrap() tells jQuery to use the DOM (Document Object Method Model) to wrap the images with the class="dropshadow" in the html inside the parenthesis.
Silly picture... but it's the same one used in the original Onion Skinned Drop Shadows:
From the jQuery site there's a link to the Ajaxian website where another javascript library was used to create the Onion Skin Drop Shadow with javascript and I think the amount of code and the complexity of the code speaks for itself. Personally, I would prefer jQuery (but you already guessed that, didn't you?)
To be fair... no one library is perfect for every job or every coder. This tutorial really isn't meant to prove that jQuery is the "One True" all purpose javascript library.
Try Prototype, Scriptaculous, YUI, Rico, Behaviour, Moo.fx and the dozens of others. If you find one that works better for you... then use it.
jQuery is a tool that spoke to me. I hope this tutorial has shown you one more useful way to use it.
jQuery makes it incredibly easy to manipulate the html generated by your visitor's browser.
You should take a moment and see what you can do with jQuery using append(), prepend(), before(), after(), html(), and remove().
For degree of difficulty, today I scored a one or a two. But that's okay because even though the concept is simple, it's applicable to lots of situations you come across in your design and coding work.
And besides, regardless of when you read this tutorial, I posted it after a Memorial Day of fun in the sun on the beaches of Florida. I just put the family to bed and I still have sand on my feet.
Rest assured, before we get to the end of the 15 Days of jQuery you're going to see some more advanced tutorials, examples, and code.
[tags]jQuery, javascript, DOM, drop shadow[/tags]
54 Responses
IkoTikashi
June 2nd, 2006 at 9:48 am
1In the paragraph “The Solution” you write:
“And then the images would be styled like so:
”
Shouldn’t you add a ‘class=”dropshadow”‘ in the img-tag?!
IkoTikashi
June 2nd, 2006 at 9:50 am
2Aye, no way to edit comments … I think you’re missing the ‘class=”dropshadow”‘ in the img-tag one paragraph above too. Thx
Jack
June 2nd, 2006 at 10:33 am
3Iko,
Good catch on your part… stupid oversight on mine.
Both fixed. Thanks.
Jörn
June 6th, 2006 at 6:35 am
4Hi Jack,
in the “A Closer Look” paragraph you wrote:
> you could do something like $(â€img#dropshadowâ€)
This is something I see too often. When referencing an ID (“#xyz”) with CSS syntax, its always redundant to put selectors in front of the ID. An ID must be unique, so it can not be specified any uniquer.
I like your tutorials (as I like JQuery), so please keep them clean
Jack
June 6th, 2006 at 8:45 am
5@Jorn,
I’m glad you pointed this out. However, I’m going to leave it as is in the tutorial for two reasons:
1- So others can see what to what you are refering
2- Even though your suggestion makes the code more concise, my syntax is not incorrect.
As an analogy… I’ve shown readers how jQuery lets you “chain” methods together and make the javascript very small, clean, and concise. But if you choose not to chain them, and write out each instruction long hand, you don’t get negative points… it still works.
Just to be clear: I agree with your point but disagree on the importance.
Mary
July 13th, 2006 at 2:55 am
6In the demo, the image doesn’t show up.
Jack
July 13th, 2006 at 9:25 am
7Mary,
Apparently the third party site took the demo down. I’ll try to get a new one back up for you.
Thom Parkin
July 28th, 2006 at 3:06 pm
8Great series. Thanks.
Is there any significance to the way you concatenated the strings in
.wrap(“” etc.?
It seems more logical to break them where you separate the opening tag from the closing tag.
Jack
July 28th, 2006 at 3:15 pm
9Thom… no significance.
Chris
August 25th, 2006 at 1:34 pm
10Hi Jack,
I know this is probably a silly question, but do you see any reason why this wouldnt work if the image was nested in a link? I’ve tried implementing this on a gallery i am working on and though its painfully simple it just doesnt seem to want to work. I have assigned a class of dropshadow to all the images, inserted the js as like the examle, checked that the jquery src link is above the js call and still nada.
Any ideas would be greatly appreciated!
Thanks,
Chris
Jack
August 25th, 2006 at 1:55 pm
11I use Firebug and Web Developer extensions for Firefox to troubleshoot. The first can give me a quick alert to goofs in my code, the second lets me view generated source, not just actual xhtml. In other words, it should show you some of the jQuery generated html.
If you’re nesting the image inside a link, I would think you’d want to target the link (instead of the image) for wrapping with the divs.
Let me know what you find.
toolman
September 19th, 2006 at 4:39 am
12Just a note re the uniqueness of css hash notation for IDs. If a CSS script is used on multiple pages or a dynamic system, it is possible that an image could share an ID with , for example, a form input ID on a different page.
ie input#unique and img#unique
Therefore, it is not always redundand if you use CSS on more then one DOM – and you should
It is unlikely, but definitely possible for IDs to match across multiple pages, especially if you have dynamic content.
jazzle
October 21st, 2006 at 7:37 am
13The demo is 404
Jack
October 22nd, 2006 at 11:00 am
14A downside of linking out to other sites for a demo. I’ll put one up on this site.
ilovejquery
November 29th, 2006 at 5:29 am
15where is the demo!!!!!
Jack
November 29th, 2006 at 11:00 am
16Right here:
http://15daysofjquery.com/examples/osds/
Matthijs
November 29th, 2006 at 1:39 pm
17Cool solution. No technique in itself is perfect but I think that if you already use jquery for something else on the same site, it’s very easy to use it for fancy dropshadows as well using your technique.
Great tutorials, thanks.
Alexis Bellido
February 1st, 2007 at 2:40 pm
18A small detail but important to notice: DOM = Document Object Model, not Method
Jack
February 1st, 2007 at 3:01 pm
19@Alexis,
Oof… you’re right! I’ll correct that silly mistake now.
Scott Chesnut
April 10th, 2007 at 12:23 pm
20Hi,
What if I wanted to get the width of the image (and the width attribute is not set in the html) and then set the width of my wrapper div accordingly? I’m thinking of a caption type situation. Also, what if my images did not have a class (i’m allowing users to upload images), but I wanted to find all images in a particular div, say a content area which does have an id or class?
Thanks,
Scott
Jack
April 10th, 2007 at 12:49 pm
21Scott,
Getting the width of an image server side with PHP or similar is easy. You can easily pass that info into your html/js.
As for the width of the div, you should at least test setting the width of the div to be less than your images, and the div should expand to contain the image, regardless of the width. A bit of a hack, but might work for you.
As for images without a class, since you’re receiving uploads, you have to be using a server side language to save and process. You could easily add a class using the same server side code. Or, if you knew the parent of the image, or it’s order in the document you could use jQuery to target the nth image.
Finding all images in a particular div is easy:
$(‘div#foo img’).each(function(){ alert(‘function goes here’); });
CSS & Javascript Image Drop Shadows by The Big Article
June 29th, 2007 at 4:47 am
22[...] http://15daysofjquery.com/wrap-it-up-lazy-mans-html-generation-with-jquery/10/ 1. Insert this code between the html tags in your header or template (header.php in WordPress). Note: You may already have jquery installed as part of your template or plugin. Try looking in /wp-includes/js/jquery/ $(document).ready(function(){ $(“img.dropshadow-right”) .wrap(” ” + “ [...]
CSS & Javascript Image Drop Shadows by The Big Article
June 29th, 2007 at 6:16 am
23[...] config and maintenance and I decided to combine Segio’s CSS only solution with a slighly more complex Javascript solution that would in the end be a bit simpler to implement and [...]
Daniel
March 17th, 2008 at 10:58 pm
24I am sorry but I keep getting this error: $(“img.dropshadow”) has no properties. Please help, I thought this might be occurring because the element hasn’t been created yet. Is that a correct assessment. The code is included in the head of the page and is a callback of the ready method just as you instructed. this is what I have in the head of the code: $(document).ready(function(){
$(“img.dropshadow”).wrap(“” + “”);
}); any pointers please?
Quin
September 26th, 2008 at 8:27 am
25How does one get jQuery to wrap a single div around all the child elements of a div?
Example:
header
Lorem ipsum
Lets say I want a single div around the , and the , how does one do it?
I tried this:
$(“div.box> *”).wrap(”);
The problem is it wraps a div around each child element. Not what I want.
Any suggestions?
Quin
September 26th, 2008 at 8:52 am
26Sorry, this blog does not seem to render code. Will look elsewhere for a answer.
Bob
November 4th, 2008 at 11:32 am
27Thank you for the very useful explanation, code example and graphics.
pligg.com
July 18th, 2009 at 2:27 am
28Wrap It Up – Lazy Man’s HTML Generation With jQuery…
I’m going to show you how easy it is to remove extraneous markup from your html source code using jQuery. Using this method will keep your code cleaner and (more importantly) will make it future layout changes much easier….
???? ?????
October 19th, 2009 at 9:39 am
29???????? ?????????. ????????? ??????? ?? ????.
Proposed Workshop schedule – jQuery « Carlos Guarany – from Toronto
March 20th, 2010 at 2:23 pm
30[...] Wrap It Up – Lazy Man’s HTML Generation With jQuery Style Sheet Switcheroo [...]
Halla Cooke
February 25th, 2011 at 11:30 am
31I am working on making a blog like %BLOGTITLE%. I still have a way to go, and certainly don’t yet get the traffic you seem to get. if you can, please give me some feedback on http://troutcatch.com as I know it needs a tune up. Nice site, sincerely, Halla Cooke
Mancation Man
April 27th, 2011 at 4:08 pm
32awesome post! thanks
walgreens coupon code
May 27th, 2011 at 3:05 pm
33Great write-up, I am regular visitor of one’s web site, maintain up the nice operate, and It’s going to be a regular visitor for a lengthy time.
fun
June 12th, 2011 at 5:21 am
34woah i like yur site. It really helped me with the info i was searching for. Appcriciate it, will save.
wonderlic questions
July 1st, 2011 at 6:48 pm
35Good stuff on here, is there a feed?
prefabrik evler
July 6th, 2011 at 8:40 am
36Just to be clear: I agree with your point but disagree on the importance.
konut projeleri
August 1st, 2011 at 7:38 am
37I agree with your point but disagree on the importance.(
motorcycle boots
August 27th, 2011 at 12:29 am
38Hi there! I just wish to give a huge thumbs up for the good info you’ve gotten right here on this post. I might be coming back to your weblog for extra soon.
gold coast
August 29th, 2011 at 9:22 pm
39I was pleased to discover it web-site.Need be towards interesting effort because of this terrific read through!! I actually undeniably enjoying just about every small amount of the software we maybe you have bookmarked as their favorite to look into different things writing.
Teodoro Mease
September 3rd, 2011 at 1:26 pm
40Thank you once more for a lot of things.
Papel de Parede
September 10th, 2011 at 12:44 pm
41We would also like to express that most individuals who find themselves with out health insurance can be students, self-employed and people who are without a job. More than half with the uninsured are really under the age of Thirty-five. They do not come to feel they are in need of health insurance since they are young plus healthy. Their income is generally spent on housing, food, as well as entertainment. Some people that do represent the working class either full or in their free time are not provided insurance through their work so they get along without as a result of rising cost of health insurance in the us. Thanks for the concepts you write about through this web site.
Samsungreality VS
September 12th, 2011 at 6:38 am
42Woah! I’m really loving the template/theme of this site. It’s simple, yet effective. A lot of times it’s challenging to get that “perfect balance” between user friendliness and visual appeal. I must say you’ve done a awesome job with this. Additionally, the blog loads extremely fast for me on Internet explorer. Exceptional Blog!
Tristan Gierhart
September 21st, 2011 at 9:35 am
43One other issue is that if you are in a problem where you don’t have a cosigner then you may want to try to make use of all of your money for college options. You can get many funds and other scholarships or grants that will present you with funding to assist with institution expenses. Thanks for the post.
android app
October 28th, 2011 at 8:22 pm
44Hi there! Do you use Twitter? I’d like to follow you if that would be ok. I’m undoubtedly enjoying your blog and appear forward to new posts.
JOCURI SLOT
November 13th, 2011 at 8:27 am
45its firs date to this blog, its nice ! keep work…..
prefabrik ev
December 4th, 2011 at 7:45 pm
46Just to be clear: I agree with your point but disagree on the importance.
webdesign
February 6th, 2012 at 10:21 pm
47Hi, Thanks for the jquery tutorial in this post. I’m new in jquery. Do you have any example for jquery contact form that integrate with captcha? Thanks
Load Central Philippines
February 24th, 2012 at 10:13 am
48http://loadcentralph.net – Your 1-stop reloading station. Buying eload has not been this easy!
KuPas
April 4th, 2012 at 8:56 am
49your blog is very unique, im very enjoy in your blog
King Palace
June 11th, 2012 at 10:42 am
50Liking it so far
[link]
September 27th, 2012 at 11:23 am
51I’ll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X, but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)
Millie
April 8th, 2013 at 6:39 pm
52Very quickly this website will be famous amid all blog
users, due to it’s fastidious posts
visit the up coming internet site
April 11th, 2013 at 5:59 pm
53In most cases they are, but when it comes to updates, patches, etc to the mmorpgs.
Don’t leave the landscape designing and terrain optimization to the last moment. As ever we strive to help our customers make informed decisions and understand where their r4 cards are coming from.
Michel Runck
May 3rd, 2013 at 10:57 am
54It’s actually a nice and useful piece of information. I am happy that you just shared this helpful information with us. Please keep us informed like this. Thanks for sharing.
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Links
15 Days Of jQuery is proudly powered by WordPress - BloggingPro theme by: Design Disease