N00b question in Javascript

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

polymorphicPinapple
Posts: 9
Joined: Fri Jan 28, 2011 9:00 pm UTC

N00b question in Javascript

Postby polymorphicPinapple » Fri Jan 28, 2011 9:54 pm UTC

Heylo guys,

I have a rather noob question in Javascript.
How can i reduce the code and make it more portable/expandable?

Code: Select all

newpage.document.form4.english.value=newpage.document.form4.english.value.replace(/<startdate>/g, startdate);
      newpage.document.form4.english.value=newpage.document.form4.english.value.replace(/<starttime>/g, starttime);
      newpage.document.form4.english.value=newpage.document.form4.english.value.replace(/<enddate>/g, enddate);
      newpage.document.form4.english.value=newpage.document.form4.english.value.replace(/<endtime>/g, endtime);
      newpage.document.form5.english.value=newpage.document.form5.english.value.replace(/<startdate>/g, startdate);
      newpage.document.form5.english.value=newpage.document.form5.english.value.replace(/<starttime>/g, starttime);
      newpage.document.form5.english.value=newpage.document.form5.english.value.replace(/<enddate>/g, enddate);
      newpage.document.form5.english.value=newpage.document.form5.english.value.replace(/<endtime>/g, endtime);
      newpage.document.form18.english.value=newpage.document.form18.english.value.replace(/<startdate>/g, startdate);
      newpage.document.form18.english.value=newpage.document.form18.english.value.replace(/<starttime>/g, starttime);
      newpage.document.form18.english.value=newpage.document.form18.english.value.replace(/<enddate>/g, enddate);
      newpage.document.form18.english.value=newpage.document.form18.english.value.replace(/<endtime>/g, endtime);


and why does this not work?

Code: Select all

var array = [4, 5, 18];
for(var i in array){
                var formname = "form"+i;
   var formobj = newpage.document.getElementsByName(formname.english);
   newpage.document.formname.english.value=formobj.value.replace(/<startdate>/g, startdate);
   newpage.document.formname.english.value=formobj.value.replace(/<starttime>/g, starttime);
}

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: N00b question in Javascript

Postby You, sir, name? » Sun Jan 30, 2011 2:29 am UTC

What does the javascript debug console say?
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

Token
Posts: 1481
Joined: Fri Dec 01, 2006 5:07 pm UTC
Location: London

Re: N00b question in Javascript

Postby Token » Sun Jan 30, 2011 2:32 am UTC

polymorphicPinapple wrote:Heylo guys,

I have a rather noob question in Javascript.
How can i reduce the code and make it more portable/expandable?

Code: Select all

newpage.document.form4.english.value=newpage.document.form4.english.value.replace(/<startdate>/g, startdate);
      newpage.document.form4.english.value=newpage.document.form4.english.value.replace(/<starttime>/g, starttime);
      newpage.document.form4.english.value=newpage.document.form4.english.value.replace(/<enddate>/g, enddate);
      newpage.document.form4.english.value=newpage.document.form4.english.value.replace(/<endtime>/g, endtime);
      newpage.document.form5.english.value=newpage.document.form5.english.value.replace(/<startdate>/g, startdate);
      newpage.document.form5.english.value=newpage.document.form5.english.value.replace(/<starttime>/g, starttime);
      newpage.document.form5.english.value=newpage.document.form5.english.value.replace(/<enddate>/g, enddate);
      newpage.document.form5.english.value=newpage.document.form5.english.value.replace(/<endtime>/g, endtime);
      newpage.document.form18.english.value=newpage.document.form18.english.value.replace(/<startdate>/g, startdate);
      newpage.document.form18.english.value=newpage.document.form18.english.value.replace(/<starttime>/g, starttime);
      newpage.document.form18.english.value=newpage.document.form18.english.value.replace(/<enddate>/g, enddate);
      newpage.document.form18.english.value=newpage.document.form18.english.value.replace(/<endtime>/g, endtime);

I might go with something like the following:

Code: Select all

function ReplaceDatesAndTimesInForm(form)
{
  ReplaceStringWithValueInForm("<startdate>", startdate, form);
  ReplaceStringWithValueInForm("<starttime>", starttime, form);
  ReplaceStringWithValueInForm("<enddate>", enddate, form);
  ReplaceStringWithValueInForm("<endtime>", endtime, form);
}

function ReplaceStringWithValueInForm(string, value, form)
{
  regExp = new RegExp(string, "g");
  form.english.value = form.english.value.replace(regExp, value);
}

ReplaceDatesAndTimesInForm(newpage.document.form4);
ReplaceDatesAndTimesInForm(newpage.document.form5);
ReplaceDatesAndTimesInForm(newpage.document.form18);

polymorphicPinapple wrote:and why does this not work?

Code: Select all

var array = [4, 5, 18];
for(var i in array){
                var formname = "form"+i;
   var formobj = newpage.document.getElementsByName(formname.english);
   newpage.document.formname.english.value=formobj.value.replace(/<startdate>/g, startdate);
   newpage.document.formname.english.value=formobj.value.replace(/<starttime>/g, starttime);
}

For starters, you're using the for-in loop wrong. The value of i will be set to the *index* of each element in the array, not the value. So it will return 0,1,2 instead of 4,5,18 - you will need to use array[i] to get the value. You're also trying to access "formname.english" when formname is just a string. And then you're trying to access "newpage.document.formname" directly instead of using the formobj you got (or didn't quite get) earlier. This might be more likely to work:

Code: Select all

var array = [4, 5, 18];
for(var i in array){
                var formname = "form"+array[i];
   var formobj = newpage.document.getElementsByName(formname);
   formobj.english.value=formobj.english.value.replace(/<startdate>/g, startdate);
   formobj.english.value=formobj.english.value.replace(/<starttime>/g, starttime);
}
All posts are works in progress. If I posted something within the last hour, chances are I'm still editing it.

keeperofdakeys
Posts: 658
Joined: Wed Oct 01, 2008 6:04 am UTC

Re: N00b question in Javascript

Postby keeperofdakeys » Sun Jan 30, 2011 8:18 am UTC

polymorphicPinapple wrote:Heylo guys,

and why does this not work?

Code: Select all

var array = [4, 5, 18];
for(var i in array){
                var formname = "form"+i;
   var formobj = newpage.document.getElementsByName(formname.english);
   newpage.document.formname.english.value=formobj.value.replace(/<startdate>/g, startdate);
   newpage.document.formname.english.value=formobj.value.replace(/<starttime>/g, starttime);
}

The variable formname holds a string, this you seem to understand. The problem is that formname in newpage.document.formname.english.value will be interpreted just as it is typed, not the contents of the variable. This may not make sense at first, but it must be this way. This is part of the command, as such it can't be changed at run-time in nearly every language in existence. Even if it could, it would be a very bad idea, as a random variable with just the right name would stop your program from working!

There are ways to do this, but they much slower and hard to do. There is almost certainly another way to achieve your goal, you just have to find it. If you tell us more about what your trying to do, someone may be able to help.

Edit: It appears the post above me has the right answer.

User avatar
Xanthir
My HERO!!!
Posts: 5334
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: N00b question in Javascript

Postby Xanthir » Sun Jan 30, 2011 8:23 am UTC

keeperofdakeys wrote:
polymorphicPinapple wrote:Heylo guys,

and why does this not work?

Code: Select all

var array = [4, 5, 18];
for(var i in array){
                var formname = "form"+i;
   var formobj = newpage.document.getElementsByName(formname.english);
   newpage.document.formname.english.value=formobj.value.replace(/<startdate>/g, startdate);
   newpage.document.formname.english.value=formobj.value.replace(/<starttime>/g, starttime);
}

The variable formname holds a string, this you seem to understand. The problem is that formname in newpage.document.formname.english.value will be interpreted just as it is typed, not the contents of the variable. This may not make sense at first, but it must be this way. This is part of the command, as such it can't be changed at run-time in nearly every language in existence. Even if it could, it would be a very bad idea, as a random variable with just the right name would stop your program from working!

There are ways to do this, but they much slower and hard to do. There is almost certainly another way to achieve your goal, you just have to find it. If you tell us more about what your trying to do, someone may be able to help.

Much slower and hard to do? Hardly.

Code: Select all

newpage.document[formname].english.value=...


The . syntax for accessing properties is just a convenient shorthand for using arrays, when you know the name of the property up-front and it doesn't have anything funny in it.

That said, this whole thing reeks of some really bad coding patterns. Can you show me the whole page, so I can recommend cleaner and easier ways to do this?
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
HarvesteR
Posts: 65
Joined: Mon May 10, 2010 1:13 pm UTC

Re: N00b question in Javascript

Postby HarvesteR » Mon Jan 31, 2011 6:33 am UTC

Why not this?

Code: Select all

var array = ["form4", "form5", "form18"];
for each (var i in array){
   newpage.document[ i ].english.value.replace(/<startdate>/g, startdate);
   newpage.document[ i ].english.value.replace(/<starttime>/g, starttime);
}


I mean, this value property is a string isn't it?

Cheers
The next sentence is the truth. - The previous sentence is a lie.

polymorphicPinapple
Posts: 9
Joined: Fri Jan 28, 2011 9:00 pm UTC

Re: N00b question in Javascript

Postby polymorphicPinapple » Mon Jan 31, 2011 1:20 pm UTC

wow thanks guys for the quick reply, it was all very helpful!

polymorphicPinapple
Posts: 9
Joined: Fri Jan 28, 2011 9:00 pm UTC

Re: N00b question in Javascript

Postby polymorphicPinapple » Mon Jan 31, 2011 1:26 pm UTC

it works!

I took all your suggestions and found out that this works

Code: Select all

var array = [4, 5, 18];
for(var i in array){
                var formname = "form"+array[i];
   newpage.document[formname].english.value=newpage.document[formname].english.value.replace(/<startdate>/g, startdate);
   newpage.document[formname].english.value=newpage.document[formname].english.value.replace(/<starttime>/g, starttime);
}

polymorphicPinapple
Posts: 9
Joined: Fri Jan 28, 2011 9:00 pm UTC

Re: N00b question in Javascript

Postby polymorphicPinapple » Mon Jan 31, 2011 1:35 pm UTC

just to explain what i originally wanted.
i had a html page with 3 (or more) forms called "form4" "form5" "form18".
in each form i also had a textarea, all called "english" (because there will be other languages)
then in each textarea i have some predetermined text containing the <startdate> and <starttime> custom tags that i want to replace once the user enters a date and time.

thanks for all your help!

ps: btw, is there a better way to use innerHTML if i have several lines of code to replace?

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6301
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: N00b question in Javascript

Postby Thesh » Tue Feb 01, 2011 4:56 am UTC

Look into document.getElementsByName(). It will give you a collection that you can loop through. Save you a lot of typing.
Summum ius, summa iniuria.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests