473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Empty Function Parameters

162 100+
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 8109
gits
5,390 Expert Mod 4TB
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 Expert Mod 8TB
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 Expert Mod 8TB
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 Expert Mod 8TB
I've seen this appoach mostly in cases, where you test for an object.
Jan 28 '09 #5
acoder
16,027 Expert Mod 8TB
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 Expert Mod 8TB
that is correct.

(20 chars min)
Jan 28 '09 #7
empiresolutions
162 100+
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
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...
1
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...
0
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...
5
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...
7
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...
13
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...
3
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...
1
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,...
10
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.