Jump to content

making a dynamic textarea name


vinnieza

Recommended Posts

Hi, sorry i'm a major newbie at javascript. Anyway, my question:

I have a form called frmProfile, a textarea called article and a button with an onclick event set to addtext(this.form)

I have this function:

[code]addtext(form){
}[/code]

With this piece of javascript in it:

[code]var Article = form.article.value;[/code]

to get what is in the text area into a variable

The problem is this, i want to use this code for multiple forms on different pages, however all my text areas have different names. Now i was wondering is there anyway to add a variable to the js code so that it's easy to change all the times i refer to form.article.value?

I tried using var textareaName = "article"; and then i could change it on each of my pages and change the js code to something like:

[code]var Article = form.textareaName.value;[/code]

But you can't do that, well it doesn't work, guessing it's because the code does not realize it's suppose to be a variable and thinks that's the name of the textarea. Is there a sort of operator that tells it that it's a variable and not the name.

Thanks :)

sorry about the title wasn't sure of how to describe my problem

Link to comment
Share on other sites

you don't have to pass the form, you can just pass the textarea element like this
[code]
onclick="addtext(this)";
[/code]

[code]
addtext(txt){
    var Article = txt.value;
}
[/code]

This way you don't have to worry about names.



Link to comment
Share on other sites

Oh, I thought the onclick event was in the textarea (dauhhh)

what you can do is access the textarea by id instead of name, this way you can either have the same id or define it in the JS in the beginning

[code]
onclick="addtext()";
[/code]

[code]

var Article = document.getElementById('ID_HERE').value;
[/code]

[code]
<textarea id="ID_HERE"></textarea>
[/code]

P.S. the id doesn't have to be the same as the name, but have to be unique.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.