freaky stuff

It's a horrendously long blog title, I know, but I wanted to mash in all the keywords I was looking for when trying to find a solution to this very sticky problem so that other folks running into the same issue can spend the next six hours doing something more productive.

jQuery makes grabbing one group of elements from a page, duplicating them and adding them somewhere else really easy. You just use a combination of clone() and something like append(), as in the following:

$('#surrounding_div').clone().appendTo('#some_other_div);

The problem with dynamically adding form elements in IE

Woot! So easy! Right, until you start goofing with forms and IE (Internet Explorer) (appropriate sound). The problem is that IE doesn't allow you to manipulate the 'name' attribute in the DOM directly. So, something like this won't work:

$('#form_element').clone().attr('name', 'new-name').appendTo('#my-form);

If you do that with a radio button, the interface will tell you that there's a problem, because since the new radio button has the same name as the original one, only one can be selected. Bugger!

Luckily, the solution is clear, but it took forever to figure it out. Instead of adjusting the name attribute directly with something like attr(), you need to edit it manually in a block of html and then add that html to the page. So, something like this would work:

$('#form_element').parent().html().
replace(/orignal_name/, 'new_name').appendTo('#my-form');

There's a couple of key points here:

  1. Because we're getting the html using $.html(), we need to move up the DOM to grab the element surrounding the element we wish to add using $.parent(), then we can grab the html using $.html().
  2. I'm using the javascript replace() function to pass a regex pattern and replacement text to the html.

What about dynamically cloning groups of inputs with jQuery?

In my particular use case, I was grabbing a set of form elements and adding them this way. There were a couple of challenges, which was that I couldn't use $.parent() to get the outer element, and the other was that the names of the form elements were being set dynamically. I learned a couple of tricks to help with this:

Use the regex modifier /g to replace all insteances of a pattern. So, when doing replace(), if I needed to replace ALL instances instead of just one, it would look like this:

replace(/original_name/g, 'new_name')

Because I couldn't use $.parent(), in the end I had to wrap an element around the html using simple contatination, like so:

var newHTML = '<div>' + $('#element').html() + '</div>';

I hope that helps you, it was sure a bugger to figure out.



Last weekend I attended DrupalCamp Colorado, and thought I should jot down a few of my personal highlights.

The Hostel

I reserved a dorm bed at a local hostel. Maybe the creepiest place I've ever been. When you register, on the counter is a ventriloquist doll, folded in half with it's legs behind his head. Next to the doll is a communal bowl of chips. Like, potato chips. BBQ flavored. Now, I appreciate the sharing attitude, but would have been far more comfortable plunging my hand into a bowl of individually wrapped candies. After a series of strange events which I hesitate to recount (just in case involved parties are tracking my posts) but which kind of pushed me over the edge of creepiness, a fellow attendee offered to let me stay at his place, and I gratefully accepted. After I presented at one session, someone who actually stayed at the hostel let me know that out of a couple of months of traveling through Europe and Asia, the Denver hostel was the dirtiest he'd seen, and I'd done good by not staying there. The result is a good story with lots of embellishments if you're (un?)fortunate enough to hear the whole tale in person.

The Sessions

I ended up attending a lot of sessions, which I haven't done at previous camps / Drupalcons. I got some good stuff out of the security session with Greg Knaddison, Ezra Gildesgame and Ben Jeavons. Also, got some D6 theming goodness, presented by Stephanie Pakrul and Jay Wolf from Top Notch Themes. The stuff going on with the Skinr module really got me excited, and hearing how TNT improved their conversion rates so drastically using just a few intuitive techniques and very little time was excellent. Ended up sitting in on a few sessions that were a little under my point on the learning curve, but I enjoyed those as well because I was watching for presentation styles, since I gave my first session ever on Sunday.

Good wifi access, fun backchannel discussion, excellent lunch with TNT people and my friend Josh Brauer. There was enough time between sessions to get some socializing done (my favorite part of Drupal events) and meet some Denver folks.

The People

I got a really good vibe from everyone at the Camp. In contrast to Drupalcon (I'm still pretty green when it comes to Drupal events), I enjoyed the atmosphere of a smaller event. Ended up talking to the same people several times, and felt less lost in the crowd. I met a lot of people I'd love to chat with again, and got to put a lot of faces to names I'd only seen in IRC. I came home seriously considering the idea of taking an extended vacation (6 months or a couple years) in the area, just to be around such an active, fun bunch of Drupal folks. The experience anchored my resolution to attend a decent-sized event at least once every couple of months, to keep connected with the really neat people that make up the Drupal community.

Giving a Presentation

I was thankful to have the opportunity to present a session, and that the audience was probably too sedated by the unlimited pizza lunch to judge me too harshly. It went well, at least from my perspective. I kept on time, didn't freak out, and managed to make a few people laugh. The goal of the session was to jumpstart folks who haven't really taken part in community discourse yet, and go over the basics of getting involved. So hopefully, a couple more folks are a little closer to making that leap, or better yet actually took it. I learned that 45 minutes is pitifully short to cover a subject in-depth, and that it would have been nice to have more time for answering questions and getting feedback about what was missing from the presentation so I can make it better next time.

Conclusionary Stuff

I'm looking forward to presenting again, got a little more excited about doing a DrupalCamp in Idaho, am considering taking my wife back to Denver to evaluate an extended vacation there, learned that if you teach something you end up believing it simply through extended exposure, was surprised at my stamina after waking up at 3am, hope to pay some hospitality forward in turn, and am looking forward to hooking up with a lot of the great folks at the camp again. Oh, and if I get back to Denver I'm totally hitting up Chedds. A grilled cheese bistro? Beautiful.



Syndicate content