Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Any simpler way to write this in javascript?
Old 03-08-2007, 10:11 PM Any simpler way to write this in javascript?
amox's Avatar
Novice Talker

Posts: 10
Name: Nash Field
Trades: 0
Hi,

I'm writing a function that simply appends email addresses into one line and separated into commas. I am a total newbie so pardon my newbie codes. So here goes my code:

Code:
                                            function getCredit(){
    var dg=document.cform;
    var fall="";
    var x=0;
    
    if (dg.femail1.value){
        fall=dg.femail1.value;
        x = x+1;
    }
     if (dg.femail2.value){
        fall=dg.femail1.value + "," + dg.femail2.value;
        x = x+1;
    }
     if (dg.femail3.value){
        fall=dg.femail1.value + "," + dg.femail2.value + "," + dg.femail3.value;
        x = x+1;
    }
     if (dg.femail4.value){
        fall=dg.femail1.value + "," + dg.femail2.value + "," + dg.femail3.value + "," + dg.femail4.value;
        x = x+1;
    }
     if (dg.femail5.value){
        fall=dg.femail1.value + "," + dg.femail2.value + "," + dg.femail3.value + "," + dg.femail4.value + "," + dg.femail5.value;
        x = x+1;
    }    
     if (dg.femail6.value){
fall=dg.femail1.value + "," + dg.femail2.value + "," + dg.femail3.value + "," + dg.femail4.value + "," + dg.femail5.value + "," + dg.femail6.value;
        x = x+1;
    }
Now if I have 10 fields it's just gonna get crazy and cumbersome. I'm sure I can write a for loop or a while loop to make this simpler but I am bad at using those loop stuff. Can someone help to simplify this code? Thanks.
amox is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-09-2007, 07:45 PM Re: Any simpler way to write this in javascript?
Ultra Talker

Posts: 256
Location: Auckland, New Zealand
Trades: 0
Well after seeing that I had to read your code to understand what you were trying to do, obviously means there's going to be a few here who wouldn't bother with it. I could take a stab in the dark and maybe get it down to like 5 or less lines but I really need to see the form before I can give you the simplest approach.

Just show me the form and I'll give you the answer, otherwise I can just see you coming back and telling me it's not working as intended.

Cheers,

MC
__________________
#------------------------------signature---------------------------------------------------------------------------------#
Quote:
I am well recognised for what I don't do than what I do. Chores are just one of those things.
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Old 03-10-2007, 02:50 PM Re: Any simpler way to write this in javascript?
Novice Talker

Posts: 7
Trades: 0
You just need a loop. Something like...

Code:
function getCredit(){
    var addresses="";

    for (var x=0; x<7; x++) {
        var e = "femail" + x.toString();
        var formvalue = document.cform.elements[e].value;
        if (formvalue) {
            if (addresses)
               addresses  = addresses + ",";
            addresses = addresses + formvalue;
    }
    return addresses;
}
I haven't tested it, but I think you get the idea.
__________________
Please help build a
Please login or register to view this content. Registration is FREE
:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE
veridicus is offline
Reply With Quote
View Public Profile Visit veridicus's homepage!
 
Old 03-11-2007, 05:29 AM Re: Any simpler way to write this in javascript?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Originally Posted by thread title
Any simpler way to write this in javascript?
the question should be "Any simpler way to write this than javascript? "

and the answer is Yes and with zero work to do.

any form fields that are named the same, will be concatenated into a single comma delimited string when submitted and retrieved at the server.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-11-2007, 10:47 PM Re: Any simpler way to write this in javascript?
amox's Avatar
Novice Talker

Posts: 10
Name: Nash Field
Trades: 0
Hi thank you so much for the responses. They are very helpful! I'm sorry I am very ambiguous in my question. Total newbie. Here's the form:

Quote:
<form method="post" action="submit2xls.asp" name="cform">
<table>
<tr>
<td align="right" valign="top" class="txt_small">Your Message:<br /></td>
<td colspan="3" align="left" valign="top" class="txt_small"><textarea name="yourmsg" cols="81" rows="3" wrap="virtual" class="txt_small" id="yourmsg"></textarea></td>
</tr>
<tr>
<td align="right" valign="top" class="txt_small">Email Address: </td>
<td width="183" align="left" valign="top"><input name="femail1" type="text" class="txt" id="femail1" size="30" /></td>
<td width="143" align="right" valign="top" class="txt_small">Email Address : </td>
<td width="183" align="left" valign="top"><input name="femail2" type="text" class="txt" id="femail2" size="27" /></td>
</tr>
<tr>
<td align="right" valign="top" class="txt_small">Email Address: </td>
<td align="left" valign="top"><input name="femail3" type="text" class="txt" id="femail3" size="30" /></td>
<td align="right" valign="top" class="txt_small">Email Address : </td>
<td align="left" valign="top"><input name="femail4" type="text" class="txt" id="femail4" size="27" /></td>
</tr>
<tr>
<td align="right" valign="top" class="txt_small">Email Address: </td>
<td align="left" valign="top"><input name="femail5" type="text" class="txt" id="femail5" size="30" /></td>
<td align="right" valign="top" class="txt_small">Email Address : </td>
<td align="left" valign="top"><input name="femail6" type="text" class="txt" id="femail6" size="27" /></td>
</tr>
<tr>
<td colspan="4" align="center" valign="top" class="txt_small"><input type="submit" name="Submit" value="Submit" onClick="getCredit()"></td>
</tr>
</table>
</form>
Eventually I'd like to use the value of variables "fall" and "x" for something else.

Last edited by amox; 03-11-2007 at 10:48 PM..
amox is offline
Reply With Quote
View Public Profile
 
Old 03-11-2007, 11:05 PM Re: Any simpler way to write this in javascript?
amox's Avatar
Novice Talker

Posts: 10
Name: Nash Field
Trades: 0
Quote:
Originally Posted by chrishirst View Post
the question should be "Any simpler way to write this than javascript? "

and the answer is Yes and with zero work to do.

any form fields that are named the same, will be concatenated into a single comma delimited string when submitted and retrieved at the server.
oh my god you're right! saves me many lines of codes!
amox is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Any simpler way to write this in javascript?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.38416 seconds with 12 queries