Making jQuery calls in a function when a parameter can be a string or integer
Consider a function of when you are creating a list of items that have unique IDs, some are long integers and some are strings since they are a mix of chars and integers. Also, you have very little control over these id values since they are retrieved from a web service and are too long and tedious to manually code. Doesn’t sound too hard of a situation to code, but it is unique enough to where a large amount of searching will occur until a solution is found.
Lets assume we have an unordered list whose <li> elements had id values are a mix between integers and strings. We also have a remove function to remove these <li> tags from the list:
<ul>
<li id="1234">
<a href="#" onclick="remove( 1234 )" ></a>
</li>
<li id="abc1234">
<a href="#" onclick="remove( abc1234 )" ></a>
</li>
</ul>
And the javascript function:
function remove( listID )
{
if ( listID === parseInt( listID ) )
$( "#" + listID ).remove();
else
$( "#" + listID.id).remove();
}
In this remove function, since we are dealing with two different types being passed into funciton, we must first determine if it is a string or integer. If it is an integer, just go ahead and use the parameter passed in as is:
if ( listID === parseInt( listID ) )
$( "#" + listID ).remove();
But if the parameter is a string, we must first get its ID property to retrieve the value being passed into the function so that we can make the jQuery call:
$( "#" + listID.id)

