473,595 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Empty Function Parameters

162 New Member
How do i declare a default value in a function? I have a simple function

Expand|Select|Wrap|Line Numbers
  1. function color_input(id,token){
  2.  
  3.     if (!empty(token)){
  4.         document.getElementById(id).style.backgroundColor = '#2A2A2A';
  5.         document.getElementById(id).style.color = '#DDC58C';
  6.     }else{
  7.         document.getElementById(id).style.backgroundColor = '';
  8.         document.getElementById(id).style.color = '';
  9.     }
  10.  
  11. }
  12.  
Called in HTML like

Expand|Select|Wrap|Line Numbers
  1. <input type="text" id="example_name" onblur="color_input('example_name');">
You will notice that the #2 parameter is missing in the html call. Sometimes i don't have it to send. in PHP i would just set the functions second param like token=''

Expand|Select|Wrap|Line Numbers
  1. function color_input(id,token=''){}
How do i set a default param in a JS function?
Jan 27 '09 #1
7 8115
gits
5,390 Recognized Expert Moderator Expert
here is a short example:

Expand|Select|Wrap|Line Numbers
  1. function foo(param) {
  2.     // when param is missing set a default-value
  3.     if (typeof param == 'undefined') {
  4.         param = 'default_value';
  5.     }
  6. }
kind regards
Jan 28 '09 #2
Dormilich
8,658 Recognized Expert Moderator Expert
another variation thereof is:
Expand|Select|Wrap|Line Numbers
  1. function foo(param) {
  2.     param = param || 'default_value';
  3. }
Jan 28 '09 #3
acoder
16,027 Recognized Expert Moderator MVP
No, that would be unreliable with any falsy arguments - see http://bytes.com/topic/javascript/an...tion-arguments
Jan 28 '09 #4
Dormilich
8,658 Recognized Expert Moderator Expert
I've seen this appoach mostly in cases, where you test for an object.
Jan 28 '09 #5
acoder
16,027 Recognized Expert Moderator MVP
It'll work in those cases, but where the variable evaluates to false, e.g. false, 0, "", it will take the default instead of the actual value passed.
Jan 28 '09 #6
Dormilich
8,658 Recognized Expert Moderator Expert
that is correct.

(20 chars min)
Jan 28 '09 #7
empiresolutions
162 New Member
thanks much for the insight.

This will be adding this to my function.
Expand|Select|Wrap|Line Numbers
  1. if (typeof token != 'undefined')
Jan 28 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
9126
by: Scott M. Lyon | last post by:
I've got a stored procedure that takes a few parameters as input parameters, and one output parameter (a uniqueidentifier). After the stored procedure call, I need to return that output parameter in a System.Guid object. Unfortunately, I'm having zero luck doing this. Public Function BuildExtract(byval ProjectNumber as string, byval ListNumber
1
3055
by: Scott M. Lyon | last post by:
I've got a stored procedure that takes a few parameters as input parameters, and one output parameter (a uniqueidentifier). After the stored procedure call, I need to return that output parameter in a System.Guid object. Unfortunately, I'm having zero luck doing this. Public Function BuildExtract(byval ProjectNumber as string, byval ListNumber
0
2636
by: jennifer.perkins | last post by:
I've seen a couple posts by people having similar problems, but the suggested solutions I've tried so far haven't worked. I'm using a SOAP client in VB.Net (constructed by wsdl.exe) and the third party web service it's consuming is served by Axis. The request messages my client sends are processed fine by the server, and the SOAP response is making its way to my client - but when .Net finishes processing the message I end up with an...
5
8351
by: Mark Olbert | last post by:
I ran into a situation this morning where the RowUpdating event of a GridView kept insisting there were no entries in either the OldValues or NewValues dictionaries. The GridView is bound to a strongly-typed collection, derived from CollectionBase, through the control's datasource property at runtime. From some googling it appears that, at least in the release candidate for NET2 this was a known bug; only binding to DataSources lets those...
7
3441
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function 'UpdateRegistrant' expects parameter '@EMail', which was not supplied. The field value was null in the database and not changed in the FormView so is null going back into the stored procedure. I'm stumped and would greatly appreciate any suggestions.
13
2840
by: lithoman | last post by:
I'm stumped here. I run the procedure Batch_Select against the database with @ID=18 and I get the expected data. When it loads into a SqlDataReader, it gets messed up somehow. Initially, after the reader.Read(), it has a row with 13 data columns, but they're all empty. So, my GetInt() function throws an error. When it jumps to the catch(), reader's columns then show the proper data. What gives? I have reader._data (which is the ID...
3
3354
by: Haleigh | last post by:
I just started a few days ago, so I'm very new to this. I'm trying to update a gridview using a store procedure. When I am in the update subroutine, newvalues and oldvalues are empty, plus count is zero. Here is the code I have. What I really want to do is get the changed value for startdate and endate just before doing an update. Thanks <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false"...
1
2312
by: =?Utf-8?B?RG9u?= | last post by:
Hello, I'm creating a web service that will allow people to enter their contact information into a SQL Server table. I get it to work when I enter all of the fields and press the invoke button, but if I leave one of the integer fields(ie StateID) empty if errors out. I tried to make the parameters optionally but it says thats not possible with a webmethod. How do you handle empty integer fields. I put the code below. Thanks
10
6898
by: arnuld | last post by:
this is from Steve Summit C notes: The empty pair of parentheses indicates that our main function accepts no arguments, that is, there isn't any information which needs to be passed in when the function is called. i think, an "empty pair" of parenthesis means a function can take unlimited number of arguments
1
4769
Soniad
by: Soniad | last post by:
Hello, I have stored procedure in which i am passing parameters (varchar(8000)), these parameters can have values or can be empty (''), i have written blocks of codes for each parameter , which executes only if parameter has values, it should not enter the block if parameter is empty, how to detect whether the parameter is empty or not. and also i am having output parameter (varchar(1)) that is returned by stored procedure, if all of...
0
7882
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8250
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3872
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1490
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1222
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.