Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Rewriting 3 Simple Utility Functions in jQuery

Posted on 2010-08-15
3
Medium Priority
?
403 Views
Last Modified: 2012-05-10
Hi All,

I'm using jQuery in a new project and wanted to re-write 3 simple utility functions using jQuery.

1. I have a handy function that I use everywhere called "isDef(arg, argType)" which checks if the argument is null or undefined, (or if "argType" is defined, is of type "argType"). Could you rewrite this using jQuery for me please?

2. I have another handy function called "el(id)" which is an exception-safe wrapper for "getElementById". Could you also rewrite this using jQuery for me please?

3. And lastly I have another handy function called "findParent(elem, type, id)" which finds the nearest parent element of elem which of the specified type and (if defined), has the specified "id". Could you also rewrite this using jQuery for me please?

I have included my orginal code for these 3 functions below:

Thanks in Advance.



// MY ORIGINAL CODE

function isDef(arg, argtype)
{
 try
 {
  if (argtype === null || typeof(argtype) == 'undefined') return (arg !== null && typeof(arg) != 'undefined');
  return (arg !== null && typeof(arg) == argtype);  }  catch (e) {}  return false; }

function el(id)
{
 try
 {
  if (!isDef(id, 'string') || id == '') return null;
  return document.getElementById(id);
 }
 catch(e) {}

 return null;
}

function findParent(elem, type, id)
{
 try
 {
  if (!isDef(elem) || !isDef(type, 'string') || type == '') return null;
  if (!isDef(id)) id = '';

  type = type.toUpperCase();

 var parent = (elem.parentNode ? elem.parentNode : (elem.parent ? elem.parent : NULL));
  while (isDef(parent) && (parent.nodeType != 1 || (parent.nodeName.toUpperCase() != type && (id == '' || id != parent.id))))
   parent = (parent.parentNode ? parent.parentNode : (parent.parent ? parent.parent : NULL));  // Select the parent of the specified type by iterating up the ancestor hiearchy
 
 if (isDef(parent)) return parent;
 }
 catch(e) {}

 return null;
}

Open in new window

0
Comment
Question by:NetSoftDS
3 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33445431
el() = $()


0
 
LVL 43

Accepted Solution

by:
David S. earned 2000 total points
ID: 33446724
jQuery is a JavaScript library, not a separate language, so there isn't always a reason to rewrite other utility functions you use to use it.  There's no need to rewrite your isDef() function.

As Michel said, $() will work in place of el(), except you would need to use "#" at the beginning of a string passed to it, if you wanted to use an ID. (Many other selectors can be used in addition to just IDs: http://api.jquery.com/category/selectors/ )

For the findParent() function, I would suggest not using it all and just use jQuery, otherwise you'd lose the ability to use jQuery's "chaining" feature.

In jQuery to get the...

parent:
  $('selector').parent()

first ancestor of a certain type:
  $('selector').parents('ul').eq(0)

ancestor with a certain ID:
  $('selector').parents('#someID').eq(0)
0
 

Author Closing Comment

by:NetSoftDS
ID: 33507708
Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question