Entries Tagged as 'jQuery'

It's ok to be cute, but not too cute.

So, on twitter yesterday, I boldly announced:

Not that anyone needs another example of jQuery's live() function, but here ya go: http://bit.ly/7tWLof

I was all proud and stuff, messing around with jQuery is always fun. I didn't check the other browsers tho. I got home and I got curious if it actually worked in all browsers and it turns out, it does not. I wasn't too concerned about the code. It was a proof of concept for a discussion at work.

I wanted to fix it, but it wasn't going to be done last night. This morning, I spent some time fixing it and I have to say I learned some things. I learned that Firefox really wants me to be successful and allowed me to be cuter than I intended. IE / Chrome really doesn't like that. Looking back on what I was doing, I can't say that I really blame the other browsers - it did feel hackish when I wrote it.

I went about fixing this, but suddenly I felt blind without having Firebug installed in IE / Chrome. Chrome's javascript debugger / console really needs some work to be anywhere useful like Firebug. So, I knew that getfirebug.com had a 'lite' version available, so I went to go check it out. It turns out, that it is just as easy as typing:

<script type="text/javascript" src="http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js"></script>

When you add that to your HTML, it's like Firebug decided to follow you around like a little puppy regardless of what browser you're in. I'm considering uninstalling Firebug from Mozilla and making that part of my template shell that I can enable/disable at will. Now, not quite so blind, I set about fixing my script.

IE / Chrome really don't like it when you use the 'click' event on a select option. Mouse{down|up} works fine (except in IE). In the end, I stuck with click() for the select box itself, rather than the option (If you want bulletproof, use TexoTela's select plug-in). So, with that in mind, rewriting everything to grab the appropriate values and text was a no-brainer.

	function addTag(){
var $tag = $(this);
var chosenValue = $tag.val();
var chosenOption = $('#'+$tag.attr('id') +' option[value='+chosenValue+']');
var chosenText = $(chosenOption).text();
var $span = $('<span id="tag_'+chosenValue+'">'+chosenText+' <a href="#" class="remove">X</a></span>');
$('#tags').append($span);
$(chosenOption).remove();
if($('#piclist option').length <= 0){ $(this).hide(); }
return false;
}

Now, the reverse... creating the option from the span was a little more challenging, but only because of the way I was building the span in the code above. I tried it many different ways, but having the anchor tag live inside the span felt, in my opinion, to be the cleanest. So, how does one get the text value of the span without including the "X" link? I'm sure I could have pulled out a regular expression, but in the end I opted to use clone() instead.

var parent_text = $parent.clone().children().remove().end().text();

I've also never used end() before either, so this was a little weird to me. So, the above is basically saying: clone the parent, traverse to the children (in this case, a href tag) and then remove it. Stop. Now tell me what the parent text is.

Perhaps there's an easier way to do all this. Regardless, I was able to get this working again in all browsers and that can be viewed on tags_example2.html. Now, one issue remains. Resorting the list appropriately. At this point, I'd probably just undo some code and let TexoTela's plug-in do majority of the work.

Btw, I left firebug lite on my 2nd example so that you can see what it looks like and how it works. Consider using it for your projects.

Some advice about diving into jQuery

I know, I know, I'm supposed to be busy switching VPS. I'm getting there. There's only so much I can do in a day. I just wanted to write a brief note about those interested in diving into jQuery. I'm probably exagerating percentages, but I want to say that 40% of jQuery development time is making sure your HTML / CSS is written correctly and as organized as you can possibly be with it.

If you spend at least that much, then you shouldn't run into any browser compatibility issues. 30% of your time is spent actually writing jQuery and 30% of your time is eyeballing the result in Firebug's console and fixing all the typos you made.

Remembering what the CSS rules are will be important. Id should be unique per page. So, if you have <a href="#" id="stuff">Text</a> - id="stuff" should not be found anywhere else. Even on a divs, paragraphs, etc. For those of us using a dynamic language (ColdFusion, PHP, etc), it can trip you up sometimes because you don't necessary think about the entire page when you're coding because you're within your application / module. I might even suggest that you come up with a naming scheme for whatever section you are in, such as id="footer_(whatever)" or id="app_row_(id)" or something to help you remember that this small chunk of code that you're looking at isn't necessarily the entire view that the end user is seeing. Classes can be used multiple times on a page. So, <a href="#" id="stuff" class="more">More Text</a> - class="more" can be used as many times as you want, even on other HTML elements.

In majority of the browsers, you can probably get away with multiple ids of the same name. In (at least) one of the browsers, it refuses to work (IE8). So, save yourself the headache. Stop rushing to get it out there and discipline yourself to writing valid (X)HTML / CSS code. jQuery will reward you for it and you'll have a long happy relationship.

What are your top 5 jQuery plugins that you use?

What are your top five plugins for jQuery that must be available when you start a new project?

I don't know about you, but I have a gazillion jQuery bookmarks that I need to wade through someday and trim that list down to something manageable. Plus, who knows how long Yahoo is going to keep floating delicious.