/    Sign up×
Community /Pin to ProfileBookmark

default values in functions

Hello

I would like to call a function with parameters, i.e. multiply(‘2′,’3′,’box1’).
In this example, the first argument would be multiplied with the second and written into the div with the id=”box1″.

My question:
How can I set a default value for the third argument? => so if I call multiply(‘2′,’3’) then it should automatically set the output-variable to ‘box1’ if not other defined in the call.

I’m sure that this is possible but I unfortunately can’t find anything about default values neither in this forum nor with google…

Thank you for the answers,
saimen

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@HaganeNoKokoroSep 08.2004 — <i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;function default&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function add(a, b, id) {

/* Here I set default values */
if(!a) a=0;
if(!b) b=0;
if(!this.id) id="di1";
/* End of default values */

if(document.getElementById(id).childNodes.length==0) document.getElementById(id).appendChild(document.createTextNode(""));
document.getElementById(id).firstChild.data=(a+b);
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="add(1);"&gt;
&lt;div id="di1"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@saimenauthorSep 08.2004 — Hello

Thank you for your answer. I've tried to apply it for my function. There is good news and bad news for me:

good news: if targetDiv is not set, then it sets the value to "calendar". that's nice :o)

bad news: if targetDiv is set, it overwrites the value with "calendar".

=> I've used the alerts to give out the values before and after the default settings...

Maybe I'm making something wrong?!

<i>
</i>function ShowCalendar(dateValue, sourceField, targetDiv) {
//alert("Funktion ShowCalendar, Variable dateValue: "+dateValue);

<i> </i>//set default variables for targetDiv
<i> </i>alert("Funktion ShowCalendar, Variable targetDiv: "+targetDiv);
<i> </i>if(!this.targetDiv) targetDiv="calendar";
<i> </i>alert("Funktion ShowCalendar, Variable targetDiv: "+targetDiv);
Copy linkTweet thisAlerts:
@javaNoobieSep 08.2004 — how are you calling this function? what are you passing to it?
Copy linkTweet thisAlerts:
@saimenauthorSep 09.2004 — The function ShowCalendar(dateValue, sourceField, targetDiv) is called from a link in html:
<i>
</i> &lt;a href="#" onclick="ShowCalendar(document.forms['formular1'].elements['startdatum'].value, 'startdatum'); return false;"&gt;
&lt;img src="calendar.gif" alt="" border="0" /&gt;
&lt;/a&gt;


The values passed are (for example):

dateValue: "2004-09-01" (or empty)

sourceField: "startdatum" (never empty)

targetDiv: "calendar2" (or empty)
Copy linkTweet thisAlerts:
@javaNoobieSep 09.2004 — remove the 'this.'
Copy linkTweet thisAlerts:
@KorSep 09.2004 — Your function has 3 parameters, but you passed only 2 on event handler...
Copy linkTweet thisAlerts:
@lwcMar 16.2010 — You can't (well, at least shouldn't) use something like
[CODE]if (!a)[/CODE]
because false is a valid value (e.g. add(false)). What you need is [URL="http://www.webdeveloper.com/forum/showthread.php?t=226401"]Javascript's equivalence of

PHP's isset[/URL]
.

I wish Javascript would just support function foobar($value = "default value") like in PHP...
×

Success!

Help @saimen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...