Click here to Skip to main content
11,790,385 members (37,724 online)
Click here to Skip to main content

jQuery UI: Samples have redundant code

, 15 Mar 2011 CPOL 6.8K 1
Rate this:
Please Sign up or sign in to vote.
Watch out for redundant code in jQuery code samples.

I have messed around a lot with .NET MVC, jQuery, and jQuery UI for some time now. I just discovered that one of the examples includes redundant code. I don't know if all of them do, but you need to watch yourself. I have seen a few sites that run jQuery examples straight up and they may have problems with this. It's really not that big of a deal, but redundant code can become a compatibility issue and it's always bad practice. You also send more data to each client and you waste bandwidth.

The example I found was the tabs control with the ability to add and remove tabs. This is the code they published:

#dialog label, #dialog input { display:block; }
#dialog label { margin-top: 0.5em; }
#dialog input, #dialog textarea { width: 95%; }
#tabs { margin-top: 1em; }
#tabs li .ui-icon-close { float: left; margin: 0.4em 0.2em 0 0; cursor: pointer; }
#add_tab { cursor: pointer; }

The problem is that it registers the submit action for the form twice. This code initializes the model popup including the form. I have highlighted the piece of code that is interesting here.

var $dialog = $( "#dialog" ).dialog({
autoOpen: false,
modal: true,
buttons: {
Add: function() {
$( this ).dialog( "close" );
Cancel: function() {
$( this ).dialog( "close" );
open: function() {
close: function() {
$form[ 0 ].reset();

That piece of code does the exact same thing as this piece of code:

var $form = $( "form", $dialog ).submit(function() {
$dialog.dialog( "close" );
return false;

The first one is a must to initialize the module popup; this is just redundant. I have played around a lot with this and I can't find any impact in functionality. If you know any or find anything, please give me a comment back, but I can't see that there would be any issues. Then in the end, I'm rather corrected and admitting that I'm wrong than running code that isn't OK.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

I develop in C# on .Net platforms like MVC. Like to use jQuery to build rich interfaces. I also blog about development and snags I got and the solutions I found for them.

I also a full time CIO at a Swedish energy company. When there is time I do some part time consulting on cloud issues.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1509028.1 | Last Updated 15 Mar 2011
Article Copyright 2011 by Kristofer Kallsbo
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid