By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,760 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,760 IT Pros & Developers. It's quick & easy.

a more compact script?

chunk1978
100+
P: 224
hi there,

i have a few rather long winded functions in my javascript, which i'm assuming could be shrunk somehow, and would like to know if someone could suggest a way to make the code more compact...

the following function allows a select menu to populate a text field according to their values. i'd like to know if there's an easier, more compact way of writing this because i have about 600 lines like this on my form :-/

thanks in advance

Expand|Select|Wrap|Line Numbers
  1. function AddHiddenPrint(val)
  2.     {
  3.     if (val=="4x6_0"){
  4.     document.IMAGECtrlForm.hidden4x6.value = "";}
  5.     if (val=="4x6_1"){
  6.     document.IMAGECtrlForm.hidden4x6.value = "1";}
  7.     if (val=="4x6_2"){
  8.     document.IMAGECtrlForm.hidden4x6.value = "2";}
  9.     if (val=="4x6_3"){
  10.     document.IMAGECtrlForm.hidden4x6.value = "3";}
  11.     if (val=="4x6_4"){
  12.     document.IMAGECtrlForm.hidden4x6.value = "4";}
  13.     if (val=="4x6_5"){
  14.     document.IMAGECtrlForm.hidden4x6.value = "5";}
  15.     if (val=="4x6_6"){
  16.     document.IMAGECtrlForm.hidden4x6.value = "6";}
  17.     if (val=="4x6_7"){
  18.     document.IMAGECtrlForm.hidden4x6.value = "7";}
  19.     if (val=="4x6_8"){
  20.     document.IMAGECtrlForm.hidden4x6.value = "8";}
  21.     if (val=="4x6_9"){
  22.     document.IMAGECtrlForm.hidden4x6.value = "9";}
  23.     if (val=="4x6_10"){
  24.     document.IMAGECtrlForm.hidden4x6.value = "10";}
  25.     if (val=="4x6_11"){
  26.     document.IMAGECtrlForm.hidden4x6.value = "11";}
  27.     if (val=="4x6_12"){
  28.     document.IMAGECtrlForm.hidden4x6.value = "12";}
  29.     if (val=="4x6_13"){
  30.     document.IMAGECtrlForm.hidden4x6.value = "13";}
  31.     if (val=="4x6_14"){
  32.     document.IMAGECtrlForm.hidden4x6.value = "14";}
  33.     if (val=="4x6_15"){
  34.     document.IMAGECtrlForm.hidden4x6.value = "15";}
  35. }
  36.  
Feb 15 '07 #1
Share this Question
Share on Google+
2 Replies

dmjpro
100+
P: 2,476
plz rewrite the function like ....

function AddHiddenPrint(val)
{
try
{
document.IMAGECtrlForm.hidden4x6.value = val.substring(val.indexOf('_')+1);
}catch(err)
{
alert(err.description);
}
}

give me the result
welcome in advance

i am online
Feb 15 '07 #2

acoder
Expert Mod 15k+
P: 16,027
One possibility:
Expand|Select|Wrap|Line Numbers
  1. function AddHiddenPrint(val) {
  2.     document.IMAGECtrlForm.hidden4x6.value = val.replace(/4x6_/,"");
  3. }
  4.  
Feb 15 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.