Hello all,
I have a javascript function that, in most cases, needs to do something with a jQuery object that I pass it. There is a single exception where the function will not need a jQuery object, but because I've written it to accept a string (the command) and a jQuery object, I need something to pass it when I call it. My function is below:
function handleNotes(command, $item) {
var $textArea = $('#textarea_' + currentDialog); // currentDialog = global var
var $notesDiv = $('#' + $item.attr('id') + "_notes");
switch (command) {
case "show":
// do something with $notesDiv and $textArea
break;
case "hide":
// do something with $notesDiv and $textArea
});
break;
case "hide only":
// do something with $textArea only
}
}
My function call where I have the problem is:
handleNotes("hide only");
I've tried handleNotes("hide only", null)
, and I've tried handleNotes("hide only", Object)
with no luck. Any ideas?
Thanks.
UPDATE
So as many people answered, it turns out I was not testing for $item being null, so it was trying to set to something each time (whether an object was passed to it or not). I changed my function code to this:
function handleNotes(command, $item) {
var $textArea = $('#textarea_' + currentDialog); // currentDialog = global var
if($item) { // if not null
var $notesDiv = $('#' + $item.attr('id') + "_notes");
}
switch (command) {
case "show":
// do something with $notesDiv and $textArea
break;
case "hide":
// do something with $notesDiv and $textArea
});
break;
case "hide only":
// do something with $textArea only
}
}
And my function call to: handleNotes("hide only", null);
Seems to work fine. And as an answer to my original question, it would appear that "null" will suffice as a blank or dummy object, or it doesn't need to be passed at all, in which case the function assigns it a null value automatically. Thanks for the responses.