www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 09-08-2004, 02:13 AM
    saimen saimen is offline
    Registered User
     
    Join Date: Sep 2004
    Posts: 3
    Question 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
    Reply With Quote
      #2  
    Old 09-08-2004, 02:24 AM
    HaganeNoKokoro's Avatar
    HaganeNoKokoro HaganeNoKokoro is offline
    Jellyfish for Brains
     
    Join Date: Jul 2004
    Location: Canada, eh
    Posts: 784
    Code:
    <html>
    <head>
    <title>function default</title>
    <script type="text/javascript">
    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);
    }
    </script>
    </head>
    <body onload="add(1);">
    <div id="di1"></div>
    </body>
    </html>
    Reply With Quote
      #3  
    Old 09-08-2004, 02:46 AM
    saimen saimen is offline
    Registered User
     
    Join Date: Sep 2004
    Posts: 3
    Post

    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?!

    Code:
    function ShowCalendar(dateValue, sourceField, targetDiv) {
    	//alert("Funktion ShowCalendar, Variable dateValue: "+dateValue);
    
    	//set default variables for targetDiv
    	alert("Funktion ShowCalendar, Variable targetDiv: "+targetDiv);
    	if(!this.targetDiv) targetDiv="calendar";
    	alert("Funktion ShowCalendar, Variable targetDiv: "+targetDiv);
    Reply With Quote
      #4  
    Old 09-08-2004, 05:07 AM
    javaNoobie javaNoobie is offline
    Java != Javascript
     
    Join Date: Jul 2004
    Location: Singapore, SG
    Posts: 654
    how are you calling this function? what are you passing to it?
    Reply With Quote
      #5  
    Old 09-09-2004, 12:53 AM
    saimen saimen is offline
    Registered User
     
    Join Date: Sep 2004
    Posts: 3
    The function ShowCalendar(dateValue, sourceField, targetDiv) is called from a link in html:
    Code:
    	<a href="#" onclick="ShowCalendar(document.forms['formular1'].elements['startdatum'].value, 'startdatum'); return false;">
    	<img src="calendar.gif" alt="" border="0" />
    	</a>
    The values passed are (for example):
    dateValue: "2004-09-01" (or empty)
    sourceField: "startdatum" (never empty)
    targetDiv: "calendar2" (or empty)
    Reply With Quote
      #6  
    Old 09-09-2004, 04:37 AM
    javaNoobie javaNoobie is offline
    Java != Javascript
     
    Join Date: Jul 2004
    Location: Singapore, SG
    Posts: 654
    remove the 'this.'
    Reply With Quote
      #7  
    Old 09-09-2004, 06:34 AM
    Kor's Avatar
    Kor Kor is offline
    Red Devil Moderator
     
    Join Date: Dec 2003
    Location: Bucharest, ROMANIA
    Posts: 12,582
    Your function has 3 parameters, but you passed only 2 on event handler...
    Reply With Quote
      #8  
    Old 03-16-2010, 03:58 AM
    lwc lwc is offline
    Registered User
     
    Join Date: Mar 2010
    Posts: 14
    You can't (well, at least shouldn't) use something like
    Code:
    if (!a)
    because false is a valid value (e.g. add(false)). What you need is Javascript's equivalence of
    PHP's isset
    .

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

    Bookmarks

    Tags
    default, function


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:07 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.