471,120 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,120 software developers and data experts.

a more compact script?

chunk1978
224 100+
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
2 916
dmjpro
2,476 2GB
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
16,027 Expert Mod 8TB
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.

Similar topics

35 posts views Thread by Mike MacSween | last post: by
13 posts views Thread by James Franklin | last post: by
7 posts views Thread by me | last post: by
5 posts views Thread by SheldonMopes | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.