Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 14th, 2005, 10:08 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:
else if (segments == 6)
                        {
                            if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value != "") && (document.forms["form"].elements["ad3"].value != "") && (document.forms["form"].elements["ad4"].value != "") && (document.forms["form"].elements["ad5"].value != "") && (document.forms["form"].elements["ad6"].value == ""))
                            {
                                el.innerHTML = el.innerHTML + seatArr[s-2];
                            }
                            if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value == "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value == "") && (document.forms["form"].elements["ad5"].value != "") && (document.forms["form"].elements["ad6"].value == ""))
                            {
                                el.innerHTML = el.innerHTML + seatArr[s-5];
                            }
                            if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value == "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value != "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value == ""))
                            {
                                el.innerHTML = el.innerHTML + seatArr[s-4];
                            }
                            if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value != "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value == "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value == ""))
                            {
                                el.innerHTML = el.innerHTML + seatArr[s-2];
                            }
                            if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value == "") && (document.forms["form"].elements["ad3"].value != "") && (document.forms["form"].elements["ad4"].value == "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value == ""))
                            {
                                el.innerHTML = el.innerHTML + seatArr[s-3];
                            }
                            if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value == "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value == "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value != ""))
                            {
                                el.innerHTML = el.innerHTML + seatArr[s-6];
                            }
                            if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value != "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value != "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value != ""))
                            {
                                el.innerHTML = el.innerHTML + seatArr[s-2];
                            }
I have written this JavaScript code which is just one condition in the function. Can anyone see a way of making it more dynamic and cutting down the code?

I cant see it,

:-(

Picco

www.crmpicco.co.uk
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
Reply With Quote
  #2 (permalink)  
Old October 14th, 2005, 11:17 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You could shorten this line:

el.innerHTML = el.innerHTML + seatArr[s-2];

to:

e1.innerHTML+= seatArr[s-2];

Not sure about the rest, looks quite long winded though.

Joe

Reply With Quote
  #3 (permalink)  
Old October 14th, 2005, 12:31 PM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , United Kingdom.
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Picco,

Try this...
Code:
// create array of empty field state
var f = document.forms["form"];
var adEmpty = new Array(6);
for(var i = 0; i < adEmpty.length; i++){
    adEmpty[i] = f.elements["ad" + (i + 1)].value == "";
}

// create array of conditions and text to append if true
var conditions = new Array(7);
conditions[0] = ["2345", seatArr[s-2]];
conditions[1] = ["5", seatArr[s-5]];
conditions[2] = ["4", seatArr[s-4]];
conditions[3] = ["2", seatArr[s-2]];
conditions[4] = ["3", seatArr[s-3]];
conditions[5] = ["6", seatArr[s-6]];
conditions[6] = ["246", seatArr[s-2]];

for(var i = 0; i < conditions.length; i++){
    var add = true;
    for(var j = 0; j < adEmpty.length; j++){
        // compare condition to state
        if((conditions[i][0].indexOf((j + 1).toString()) == -1) != adEmpty[j]){
            add = false;
            break;
        }
    }
    if(add){
        el.innerHTML += conditions[i][1];
    }
}
HTH,

Chris
Reply With Quote
  #4 (permalink)  
Old October 17th, 2005, 05:40 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

Thanks Chris, that code looks interesting. I'll bring that into my code.
Thanks Joe, added your code as well. Cheers:

Code:
el.innerHTML += seatArr[s-parseFloat(control)]

www.crmpicco.co.uk
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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 1 September 27th, 2005 11:44 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



All times are GMT -4. The time now is 05:39 AM.


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