/    Sign up×
Community /Pin to ProfileBookmark

Empty Function Paramaters

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

[CODE]
function color_input(id,token){

if (!empty(token)){
document.getElementById(id).style.backgroundColor = ‘#2A2A2A’;
document.getElementById(id).style.color = ‘#DDC58C’;
}else{
document.getElementById(id).style.backgroundColor = ”;
document.getElementById(id).style.color = ”;
}

}
[/CODE]

Called in HTML like

[code=html]<input type=”text” id=”example_name” onblur=”color_input(‘example_name’);”>[/code]

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=”

[CODE]function color_input(id,token=”){}[/CODE]

How do i set a default param in a JS function?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@voidvectorJan 28.2009 — in the function itself just check for if token is defined:
<i>
</i>if (typeof token != 'undefined')


JavaScript doesn't enforce parameter typing/count, you manage it yourself, any excess parameter is discarded and any missing parameter is treated as undefined.
Copy linkTweet thisAlerts:
@cesarcesarauthorJan 28.2009 — @voidvector - perfect thanks.
×

Success!

Help @cesarcesar 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.6,
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,
)...