Published
Sep 2, 2009
|
In Javascript/jQuery
Well sometime back, I faced this unique problem. It took me sometime to solve it, so I thought of documenting it as my learning so that it is useful for others and might help me one when I need to do it again.
Problem
Okay, the following is the requirement. You have a web page with a set of text boxes. The number of text-boxes might vary based on some backend logic. You need to make sure the user doesn’t enter duplicate values in these set of textboxes. He can leave them blank, but cannot enter duplicate values.
Solution
The following is my solution.
function findDuplicates() {
var isDuplicate = false;
jQuery("input[name^='access_keys']").each(function (i,el1) {
var current_val = jQuery(el1).val();
if (current_val != "") {
jQuery("input[name^='access_keys']").each(function (i,el2) {
if (jQuery(el2).val() == current_val && jQuery(el1).attr("name") != jQuery(el2).attr("name")) {
isDuplicate = true;
jQuery(el2).css("background-color", "yellow");
jQuery(el1).css("background-color", "yellow");
return;
}
});
}
});
if (isDuplicate) {
alert ("Duplicate values found.");
return false;
} else {
return true;
}
}
I basically loop through all the text boxes and compare the values with all the other textboxes, in nested loops. Basically it is like bubble sort. I know this is bad, but was not able to think of any efficient way of doing it. Do you guys can think of a better way?
Posted in Javascript/jQuery
|
Tagged Javascript/jQuery, jQuery, Web Developer
|
Published
Feb 27, 2009
|
In Javascript/jQuery
Regular readers of this blog will know that I am great fan of jQuery. Recently I am thinking of evangelizing jQuery at work and convince people who take up decision to make jQuery as the default JavaScript framework to be used across the organization.
The following is the list of advantages/benefits which I have prepared so far.
- Light weight and has a very small footprint.
- Browser abstraction – jQuery provides browser abstraction and is hightly optimized for each individual browser.
- Excellent Plugin architecture – jQuery has an excellent Plugin architecture and you can find a jQuery Plugin for anything that you want to do.
- Speed – When compared with other libraries, jQuery is much faster.
- Less code == Less mistakes == better performance.
- Active development community – It has a very active development community headed by John Resig
- Backed by major organizations like Microsoft and Nokia. Microsoft had integrated it with the latest version of ASP.NET
- Very small learning curve.
- Chaining well suited for designers since it uses selectors based on CSS Selectors.
- Excellent Documentation for all methods and selectors.
- jQuery UI – Provides ready made themable GUI components.
Let me know if you could think of any addition to this list. Thanks
Posted in Javascript/jQuery
|
Tagged Javascript/jQuery, jQuery
|
Published
Feb 6, 2009
|
In Javascript/jQuery
I just finished viewing John Resig’s talk titled “DOM is a mess” at Yahoo. I took some notes while watching the video and I am posting them here, so that I can refer to them at a later point of time (It is easier to search when it is not on paper
). Also it might help someone to get the outline of the talk before actually watching the video. The original video runs for more than an hour.
Also be warned that the following is my own interpretation of the video and I might have missed or could have interpreted some point differently.
About the speaker, John Resig
As you all know John Resig is the creator of the excellent jQuery library. He works for Mozilla corporation and you can get more information about him from his blog.
DOM is a mess
This is the first thing John Resig said about DOM methods after saying that DOM is a messy
Nearly every DOM method is broken in some way, in some browser.
The following are some of the bugs in the DOM methods
getElementByID ()
IE and Old versions of Opera return elements whose name == id
getElementByTagName ()
.length gets overwritten in IE if an element with an ID = “length” is found
getElementsByClassName ()
Opera doesn’t match a second specified class
querySelectorAll ()
Safari 3.2 can’t match uppercase characters in quirks mode.
So the moral is that almost every method in DOM is messed up.
Writing Cross-browser code
Find out the cost/benefit ratio for supporting a browser and then pick the browsers you are going to support before writing your code.
He talked about Yahoo’s graded support and jQuery browser Support.
Escaping from DOM’s mess
The following are some of the tips to escape from DOM’s mess.
- Having a good test suite is not a facility but a requirement.
- Don’t introduce global variables or extend native objects.
- The order in which style sheets are included matters.
- Don’t use browser sniffing, but use Object detection or feature simulation instead.
- Don’t assume a browser will always have a bug. They might get fixed in a future release.
- Gracefully degrade for old browsers
- As your code matures, the number of assumptions should reduce.
- While removing elements from DOM, clean it by unbinding the events
Links
So my dear readers what you think about my notes. Also let me know if you like notes for videos in this format. If there is a demand, then I can post some of my notes on other videos which I have already viewed.
Posted in Javascript/jQuery
|
Tagged DOM, Javascript/jQuery, John Resig, jQuery, Notes, Yahoo
|
Published
Mar 6, 2008
|
In Javascript/jQuery
I found lot of useful links related to jQuery while researching for my presentation on jQuery. I thought of sharing them here so that it will be useful for others who are looking to get their feet wet with jQuery.
I am planning to constantly updated this post, so if you have any other links which might be useful do leave a comment and I will add them.
Homepage
References
Tutorial/Articles
jQuery related slides
Some useful Plugins
jQuery Related Books
Posted in Javascript/jQuery
|
Tagged Drupal, Javascript/jQuery, jQuery
|
Published
Sep 16, 2006
|
In Web Programming
I was just wondering how the result array of the getElementsByTagName function will be sorted. Will it be elements of the array be sorted in the order in which they appear in the document or is it completely random? And how does it behave in different browsers?
Can anyone shed more lights on this or provide me some pointers?
Self Note (by my evil twin): Sudar, you have become lazier these days. If you are not getting a response take the pain to test it across different browses by yourself!!
Posted in Web Programming
|
Tagged DOM, getElementsByTagName, Javascript/jQuery
|
Published
Jun 4, 2006
|
In Web Programming
Well I am going to flirt with an Internet Meme which is getting spread around. It all started when Emil wrote Levels of CSS knowledge. This inspired Roger to write Levels of HTML knowledge and that got followed by Levels of Javascript knowledge, the levels of Accessibility knowledge etc.
Instead of leaving a comment on each blog separately I decided to write about my level of knowledge here. So here it goes
Apart from letting you know where you are standing, these posts are really fun to read. So what is your Level of web Knowledge?
Posted in Web Programming
|
Tagged Accessibility, css, html, Javascript/jQuery, meme
|
Published
Nov 9, 2004
|
In Javascript/jQuery
Check out yxScripts.com. Excellent ready to use java script components like Menu, calendar etc.. Really damn good.
Posted in Javascript/jQuery
|
Tagged Components, Javascript/jQuery
|
Published
Nov 4, 2004
|
In Javascript/jQuery
JavaScript Toolbox – Reusable Libraries And Objects
This is a collection of Javascript source files, examples, and concepts. Use these instead of always re-inventing JavaScript solutions.
Posted in Javascript/jQuery
|
Tagged Javascript/jQuery, Toolbox
|