Reputation: 23
I've been trying to call a javascript function which may accept 3 parameters, namely: pcode, color and size. Pcode is always supplied. However, color and size may or may not be supplied. Parameters supplied may be: a.) pcode only b.) pcode and color c.) pcode and size d.) pcode color and size.
Pcode is integer. Color and size are strings.
I've checked that the onclick='offline(11,Pink,Small)'
are supplied correctly, but the function does not execute itself.
function offline(pcode,color,size)
{
if ((typeof color !== undefined)&&(typeof size === undefined))
{
$("#offlinecart").show();
$("#offlinecart").load('addoffline.php?orderno='+orderno+'pcode='+pcode);
alert(" Product successfully added.");
}
else if ((typeof color !== undefined)&&(typeof size === undefined))
{
$("#offlinecart").show();
$("#offlinecart").load('addoffline.php?orderno='+orderno+'pcode='+pcode+'color='+color);
alert(" Product successfully added.");
}
else if ((typeof color === undefined)&&(typeof size !== undefined))
{
$("#offlinecart").show();
$("#offlinecart").load('addoffline.php?orderno='+orderno+'pcode='+pcode+'size='+size);
alert(" Product successfully added.");
}
else if ((typeof color !== undefined)&&(typeof size !== undefined))
{
$("#offlinecart").show();
$("#offlinecart").load('addoffline.php?orderno='+orderno+'pcode='+pcode+'color='+color+'size='+size);
alert(" Product successfully added.");
}
}
Upvotes: 0
Views: 162
Reputation: 7666
Since you using jquery, why not make a click event rather than having onclick inline event, something like this,
$(document).on('click','#offlineId',function(){
if ((typeof color !== undefined)&&(typeof size === undefined))
{
$("#offlinecart").show();
$("#offlinecart").load('addoffline.php?orderno='+orderno+'pcode='+pcode);
alert(" Product successfully added.");
}
else if ((typeof color !== undefined)&&(typeof size === undefined))
{
$("#offlinecart").show();
$("#offlinecart").load('addoffline.php?orderno='+orderno+'pcode='+pcode+'color='+color);
alert(" Product successfully added.");
}
else if ((typeof color === undefined)&&(typeof size !== undefined))
{
$("#offlinecart").show();
$("#offlinecart").load('addoffline.php?orderno='+orderno+'pcode='+pcode+'size='+size);
alert(" Product successfully added.");
}
else if ((typeof color !== undefined)&&(typeof size !== undefined))
{
$("#offlinecart").show();
$("#offlinecart").load('addoffline.php?orderno='+orderno+'pcode='+pcode+'color='+color+'size='+size);
alert(" Product successfully added.");
}});
Upvotes: 0
Reputation: 483
When passing strings as parameter into function, string have to be enclosed within ' or " quotes.
Try like this
onclick='offline(11,"Pink","Small")'
or
onclick="offline(11,'Pink','Small')"
Upvotes: 2
Reputation: 449
If you're just looking for a debugging method, you can use the arguments
object. Every function has it an invocation. It acts like an Array
, although does not have all of the native methods.
For example, in order to check the first three arguments passed, you can use:
var firstThree = Array.prototype.slice.apply(arguments, 0, 3);
Or, to check the number of arguments given, add this within your function:
console.log(arguments.length)
You can see more details on the object over at mdn.
Usually, if something is undefined
, it was not passed in. The exception would be if undefined
were to be passed.
This is also often a case where default operators are used. For example, to check if something was passed, and supply it if it was not:
var passedArg = passedArg || defaultArg;
That being said, it'd be useful to see the code calling this. It seems to be an event handler, which could have other issues with it. Could you add that to the post?
Upvotes: 0