{"id":1051,"date":"2008-12-19T07:08:51","date_gmt":"2008-12-19T14:08:51","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=1051"},"modified":"2008-12-19T07:08:51","modified_gmt":"2008-12-19T14:08:51","slug":"jquery-tag-wrapping","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/jquery-tag-wrapping\/","title":{"rendered":"jQuery tag wrapping"},"content":{"rendered":"<p>Many times you&#8217;ll be working on something in HTML that could be long and repetitive, and then for some reason you need to edit the entire thing to wrap each of the tags on some other tags, a common example would be to link many elements.<\/p>\n<p>Take this example, there&#8217;s a bunch of photos, they&#8217;re all contained in divs that have a css class called &#8220;photos&#8221;, inside we just throw<br \/>\na bunch of &lt;img> tags, and all of a sudden our boss tells us he wans to have every image on that page linked to &#8220;http:\/\/domain.com\/photos&#8221;. jQuery to the rescue, no need to wrap each &lt;img> with &lt;a href=&#8221;&#8221;> by hand, you can do it all in one line<\/p>\n<p>So if the code looks like this:<\/p>\n<pre>\n       &lt;div class=\"photos\">\n            &lt;img src=\"http:\/\/domain.com\/0670027481_m.jpg\"\/>\n            &lt;img src=\"http:\/\/domain.com\/211161aa99_m.jpg\"\/>\n            &lt;img src=\"http:\/\/domain.com\/173bb0cd6_m.jpg\"\/>\n        &lt;\/div>\n        &lt;div class=\"photos\">\n            &lt;img src=\"http:\/\/domain.com\/841fd90b5b_m.jpg\"\/>\n            &lt;img src=\"http:\/\/domain.com\/52dda2cee5_m.jpg\"\/>\n            &lt;img src=\"http:\/\/domain.com\/b569399599_m.jpg\"\/>\n        &lt;\/div>\n        &lt;div class=\"photos\">\n            &lt;img src=\"http:\/\/domain.com\/8806e863a4_m.jpg\"\/>\n            &lt;img src=\"http:\/\/domain.com\/5e43aa95fe_m.jpg\"\/>\n            &lt;img src=\"http:\/\/domain.com\/68a74a088c_m.jpg\"\/>\n        &lt;\/div>\n\n        &lt;div class=\"tshirt-photos\">\n            &lt;img src=\"http:\/\/farm4.static.flickr.com\/3149\/3113619519_72ce82c545_m.jpg\"\/>\n            &lt;img src=\"http:\/\/farm4.static.flickr.com\/3236\/3105792155_99f2388869_m.jpg\"\/>\n            &lt;img src=\"http:\/\/farm3.static.flickr.com\/2114\/2372582266_765842fae9_m.jpg\"\/>\n        &lt;\/div>\n<\/pre>\n<p>With jQuery you can match all the &lt;img> elements, and wrap them all with &#8220;&lt;a href=&#8221;http:\/\/domain.com\/photos&#8221;>&lt;\/a>&#8221; in a single line of code:<\/p>\n<pre>\n$(document).ready(function() { $(\".photos img\").wrap('&lt;a href=\"http:\/\/domain.com\/photos\"><\/a>') })\n<\/pre>\n<p>So to explain how it works, when the document is ready (when it&#8217;s finished loading) the function inside is called back, it will use the jQuery selector <strong>$(&#8220;.photos img&#8221;)<\/strong> to match all the <strong>&lt;img><\/strong> tags contained withing elements of css class &#8220;photos&#8221;, then it applies the wrap() function, which will wrap the matched elements with the given html tags.<\/p>\n<h2>About jQuery<\/h2>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/marcgrabanski.com\/img\/jQuery-logo.gif?w=640\" class=\"alignleft\"\/>jQuery is a lightweight JavaScript library that emphasizes interaction between JavaScript and HTML. It was released January 2006 at BarCamp NYC by John Resig.<\/p>\n<p>Dual licensed under the MIT License and the GNU General Public License, jQuery is free and open source software.<\/p>\n<p>jQuery contains the following features:<\/p>\n<ul>\n<li>DOM element selections\n<\/li>\n<li>DOM traversal and modification, (including support for CSS 1-3 and basic XPath)<\/li>\n<li>Events<\/li>\n<li>CSS manipulation<\/li>\n<li>Effects and animations<\/li>\n<li>Ajax<\/li>\n<li>Extensibility<\/li>\n<li>Utilities &#8211; such as browser version and the each function.<\/li>\n<li>JavaScript Plugins<\/li>\n<\/ul>\n<p><a href=\"http:\/\/docs.jquery.com\/Main_Page\" rel=\"none\">jQuery Documentation<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many times you&#8217;ll be working on something in HTML that could be long and repetitive, and then for some reason you need to edit the entire thing to wrap each of the tags on some other tags, a common example would be to link many elements. Take this example, there&#8217;s a bunch of photos, they&#8217;re [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15,30],"tags":[1437,536,585,592,737,1093,1123],"class_list":["post-1051","post","type-post","status-publish","format-standard","hentry","category-code","category-geeklife","tag-code","tag-html","tag-javascript","tag-jquery","tag-one-liners","tag-web-development","tag-wrapping"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-gX","jetpack-related-posts":[{"id":2233,"url":"https:\/\/www.gubatron.com\/blog\/twitterautolinks-jquery-extension-to-replace-twitter-names-for-links-to-their-respective-pages\/","url_meta":{"origin":1051,"position":0},"title":"twitterAutoLinks: jQuery extension to replace twitter @names for links to their respective pages.","author":"gubatron","date":"August 2, 2011","format":false,"excerpt":"Save the following on a .js file, preferably named jquery.twitterAutoLinks.js [javascript] \/** Auto replace all Twitter nicknames for links *\/ $.fn.twitterAutoLinks = function() { return this.each(function() { var html = $(this).html(); $(this).html(html.replace(\/B@([w-]+)\/gm, '<a href=\"http:\/\/twitter.com\/$1\" target=\"_blank\">@$1<\/a>')); }); }; [\/javascript] Usage After importing the jquery.twitterAutoLinks.js file (make sure it's after you've imported\u2026","rel":"","context":"In &quot;AJAX&quot;","block_context":{"text":"AJAX","link":"https:\/\/www.gubatron.com\/blog\/category\/ajax\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":402,"url":"https:\/\/www.gubatron.com\/blog\/an-useful-example-on-how-to-extend-javascript\/","url_meta":{"origin":1051,"position":1},"title":"An useful example on how to extend Javascript","author":"gubatron","date":"November 29, 2006","format":false,"excerpt":"Today I found out I could extend the functionality of __EXISTING__ html classes with javascript. I was manipulating DOM objects, better known as HTMLElement objects, and I needed a way to print the HTML that represents the tag of the object, not what's contained by the tags... a friend told\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":172,"url":"https:\/\/www.gubatron.com\/blog\/fun-with-sql-left-joins\/","url_meta":{"origin":1051,"position":2},"title":"Fun with SQL Left Joins","author":"gubatron","date":"June 22, 2005","format":false,"excerpt":"Today I spent a great deal of time trying to pull something in one fancy SQL statement. In the end, the words of my first algorithm teacher and common sense made me desist from making such a fancy SQL statement, which I still believe can be made... The problem: I\u2026","rel":"","context":"In &quot;Gubatron&quot;","block_context":{"text":"Gubatron","link":"https:\/\/www.gubatron.com\/blog\/category\/gubatron\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1324,"url":"https:\/\/www.gubatron.com\/blog\/html-5-is-out-and-about\/","url_meta":{"origin":1051,"position":3},"title":"HTML 5 is out and about","author":"gubatron","date":"July 11, 2009","format":false,"excerpt":"I believe that the <canvas>, <audio>and <video> tags will make the web a pretty exciting place. A lot of Flash components will be rewritten or converted into Javascript+HTML5 Object components making available more reusable elements for a graphical and interactive web, all being open sourced (javascript) and with no extra\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2186,"url":"https:\/\/www.gubatron.com\/blog\/new-location-of-src-jar-for-jdk-1-6-0_24-on-mac-os-x\/","url_meta":{"origin":1051,"position":4},"title":"New Location of src.jar for JDK 1.6.0_24 on Mac OS X","author":"gubatron","date":"March 5, 2011","format":false,"excerpt":"These Apple devs... why do you keep moving src.jar? Here's the new location: [bash]\/Library\/Java\/JavaVirtualMachines\/1.6.0_24-b07-330.jdk\/Contents\/Home\/src.jar[\/bash] Not there! If you don't see src.jar there, and you're running Mac OSX 10.6, and you did download the Java system update, you will have to go to developer.apple.com and download the \"Java for Mac OS\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4135,"url":"https:\/\/www.gubatron.com\/blog\/the-curious-case-of-inconsistent-cargo-fmt-formatting-and-how-to-fix-it\/","url_meta":{"origin":1051,"position":5},"title":"The Curious Case of Inconsistent cargo fmt Formatting (and How to Fix It)","author":"gubatron","date":"February 17, 2025","format":false,"excerpt":"Have you ever run into a situation where\u00a0cargo fmt, Rust's code formatter, produces different output on different machines, even though you're working on the same project? This can be incredibly frustrating, especially when you're trying to maintain consistent code style across a team or between your own development environments. I\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Abstract_digital_art_fragmented_code_snippets_floati_e3f2c486-78ad-4b2a-b86a-e792a1970064_2.png?fit=1024%2C1024&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Abstract_digital_art_fragmented_code_snippets_floati_e3f2c486-78ad-4b2a-b86a-e792a1970064_2.png?fit=1024%2C1024&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Abstract_digital_art_fragmented_code_snippets_floati_e3f2c486-78ad-4b2a-b86a-e792a1970064_2.png?fit=1024%2C1024&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Abstract_digital_art_fragmented_code_snippets_floati_e3f2c486-78ad-4b2a-b86a-e792a1970064_2.png?fit=1024%2C1024&ssl=1&resize=700%2C400 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/1051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=1051"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/1051\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=1051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=1051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=1051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}