Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 27th, 2005, 06:28 AM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default make javascript code more dynamic

Code:
        noinf = document.form.noi.value;

        if(noinf==2)
        {
            var inf_length2 = document.form.birthday2.value.length

            if((Math.ceil(inf_length2)<8)&&(Math.ceil(inf_length2)!=0))
            {
                var do_not_check_full_dob = true;
            }
            if(Math.ceil(inf_length2)==8)
            {
                var do_not_check_full_dob = false;
            }

        }

        if(noinf==3)
        {
            var inf_length3 = document.form.birthday3.value.length

            if((Math.ceil(inf_length3)<8)&&(Math.ceil(inf_length3)!=0))
            {
                var do_not_check_full_dob = true;
            }
            if(Math.ceil(inf_length3)==8)
            {
                var do_not_check_full_dob = false;
            }

        }

        if(noinf==4)
        {
            var inf_length4 = document.form.birthday4.value.length

            if((Math.ceil(inf_length4)<8)&&(Math.ceil(inf_length4)!=0))
            {
                var do_not_check_full_dob = true;
            }
            if(Math.ceil(inf_length4)==8)
            {
                var do_not_check_full_dob = false;
            }

        }
I am trying to make my code run as smoothly as possible, is there a way to make this code more dynamic? Put it in a loop or something?

Picco

www.crmpicco.co.uk
www.crmpicco.co.uk.tt
www.milklemonadechocolate.uk.tt
www.griswolds.uk.tt
www.piccosmini.co.uk.tt
www.morton.uk.tt
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
  #2 (permalink)  
Old September 27th, 2005, 11:44 AM
Authorized User
 
Join Date: Jul 2005
Location: New York, New York, .
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Based upon what you wrote, I think you meant to check the dob.
1st: I would use switch
2nd: Write up a function to get the value length
3rd: Conditional operator

function yourFunc(){
noinf = document.form.noi.value;

if noinf != "" {
  var inf_length = getValueLength('birthday'+ noinf);
  do_not_check_full_dob = (inf_length > 0) ?
       ( (inf_length < 8)? true: false) : false;

} // if noinf != ""

 .... your other operations...

} // end yourFunc()

}

/** get the length */
function getValueLength(controlID){
  if (document.form.controlID) {
    return document.form.controlID.value.length;
  }
}




Similar Threads
Thread Thread Starter Forum Replies Last Post
build switch statement to make dynamic crmpicco Javascript How-To 0 October 27th, 2005 05:44 AM
make javascript code more dynamic crmpicco Javascript How-To 3 October 17th, 2005 05:40 AM
Can JavaScript Make Recommendations? angela18cali Javascript 1 November 16th, 2004 04:24 AM
Can JavaScript make recommendations? angela18cali Javascript How-To 0 November 15th, 2004 07:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.