Dynamic form names in Javascript

They have: 9 posts

Joined: Nov 2003

Hi, I have two separate forms named "content_form" and "event_form". I used to have only one form, content_form. So, this statement worked:
document.content_form.content.value += "<b>" + formatted_text + "</b>";'
Okay, that works. Now, I want to replace "content_form" with "form_name" and insert it dynamically based on whether the "content_form" or "event_form" is on the current page. I tried this:

if(document.content_form)
  form_name = 'content_form';
else if(document.event_form)
  form_name = 'event_form';

document.form_name.content.value += "<p>" + formatted_text + "</p>";
'

But this does not work. What am I missing here? TIA!

They have: 5,633 posts

Joined: Jan 1970

Ouch! that script is quite far from useing web standerds... Have you tried viewing it in Firefox? You'll find much more that that isnt working.

So in order to put you back on the right track. I need to know a little more.
1) Are you just inserting text?
2) Why are you putting it in a form?

lol, I just noticed you arnt useing brackets. Try this...

if(document.content_form){
  form_name = 'content_form';
}else if(document.event_form){
  form_name = 'event_form';
}
'

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

if(document.content_form) and if(document.event_form only check for the presence of those two forms, not whether one or the other is the active form. That is, unless this snippet is in a function that we can't see.

You'll have to show us a lot more code, or better yet, a test page, so we can see your exact setup and what you're trying to do. Also, what do you mean when you say it doesn't work? What error do you get, if any? How does the script behave?

There's a few other changes you'll probably need to make too, such as using the appropriate getElementById() selector rather than directly accessing the form from document. More on that later.

Smiling

They have: 9 posts

Joined: Nov 2003

Thanks for the responses! Let me explain a little more what I'm trying to do. I have a form that lets users click on a formatting button to insert formatted text into a textarea. It works very much like the "Guided mode" of this forum software. Click on the "B" and you get a pop-up that accepts text. When the user is done, [b] tags get placed around what they typed.

It works perfectly fine right now. However, I wanted to add a form on another page, and reuse the javascript code that I've already created. That is why I was trying to capture the form name. I didn't know how to do that, which is why I was using:

if(document.content_form)
  form_name = 'content_form';
else if(document.event_form)
  form_name = 'event_form';
'
I know this isn't the best method, as it would be better if I didn't have to add lines of code every time I wanted to reused the JavaScript. Maybe someone can suggest how to do that.

Now, once I have the form name, I need to insert the form name in my next line of code:
document.content_form.content.value += "<p>" + formatted_text + "</p>";'
Here, "content_form" should be whatever form name was captured in the previous step. This line of code places [p] tags around the entered text and inserts it into the textarea.

I hope that clarifies things. This should be very easy to do.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.