PDA

View Full Version : variable command ()



arfa
Apr 5th, 2012, 12:57 AM
How to set a js command using a variable?

var name = 'field';
var number = '1';

name.number(); doesn't work [when the js function is called field1]

I am using a set of dynamically created textareas and need to be able to reference each one by number.

Any thoughts?

xelawho
Apr 5th, 2012, 01:20 AM
surely there's a better way to do what you are trying to do, but this works:


var name = 'field';
var number = '1';
window[name+number]=function(){alert("hi")}
field1();

Old Pedant
Apr 5th, 2012, 02:08 AM
You didn't ready the important part of that, Xelawho:


I am using a set of dynamically created textareas and need to be able to reference each one by number.


MUCH easier problem than trying to reference a *function* dynamically.

***********

If you have

<form id="myForm">
...
<textarea name="ta1" id="field1"></textarea>
<textarea name="ta2" id="field2"></textarea>
<textarea name="ta3" id="field3"></textarea>
...
</form>

Then you have two choices:


var taNum = ...get a number somehow...
var taRef = document.getElementById("field" + tanum); // using the id

or


var taNum = ...get a number somehow...
var form = document.getElementById("myForm");
var taref = form.elements["ta" + tanum]; // using the name

xelawho
Apr 5th, 2012, 02:31 AM
I think I did read that. But don't they want a different function for each textarea?

I dunno... like I said, there's surely a better way of doing it than my suggestion, but that seemed to be what the question was

Old Pedant
Apr 5th, 2012, 03:27 AM
> But don't they want a different function for each textarea?

Hmmm...sure could read it that way, indeed. Wouldn't *THAT* be a monstrous mistake? Hope that was a typo and he/she isn't really doing it that way.

Logic Ali
Apr 5th, 2012, 05:13 AM
I am using a set of dynamically created textareas and need to be able to reference each one by number.

Presumably each is assigned a name or ID with a sequential numeric suffix.

To reference by name via a parent form: myForm.elements[ 'prefix' + n ]; myForm being a form reference, not a name or ID.

To reference by ID: document.getElementById( 'prefix' + n );

Old Pedant
Apr 5th, 2012, 08:28 AM
Presumably each is assigned a name or ID with a sequential numeric suffix.

To reference by name via a parent form: myForm.elements[ 'prefix' + n ]; myForm being a form reference, not a name or ID.

To reference by ID: document.getElementById( 'prefix' + n );


Gee, that code looks familiar. <grin/>

arfa
Apr 5th, 2012, 02:46 PM
Wow, a wealth of offerings. A great thread indeed. Thank you.

Your interest encourages me to reveal the full extent of my quandry...
Read on at your peril. I will, in the interim, read your generous suggestions.

1 - tis a 'he'
2 - he has OK php but a bit js limited - nah, js is pretty bad :(
3 - not young
4 - has indeed been generating "sequential numeric suffix."

I am modifying my (PHP) CMS to include a WYSIWIG editor (punyMCE - js)
puny has limited function and I am trying to bolt on a color picker (jscolor) to add text color & bg options
Both these are excellent, free scripts.
The CMS is built for remote www connections - dialup, satellite... so needs to be small.

The editor toolbar needs individual instances for each textarea. These are 'grabbed' as iframes by the editor.
> At the moment I am trying to use the on.close.command (in the picker) to activate the relevant numbered js. to take the selection (using ed. command) and modify (pulling id.value from the picker unput.

With sequential js for each editor instance I just need to have the picker know which instance is being modified. This is *really* my question - assuming that I continue with sequential numbering. I am keen to learn and open to alternatives.

I hope that this makes things more clear.
I will wait before creating an example.

thanks again

Old Pedant
Apr 5th, 2012, 09:36 PM
I don't think you need to wait. I think we gave you basically the right answers.

Sequential numbering is fine. Using document.getElementById() as we showed is fine.

You don't need to create dynamic references to the functions, so far as I can see, so you are likely on the right track.

arfa
Apr 6th, 2012, 12:39 AM
Yes, I have made some head way and a working solution feels close to hand.

As I said - the *real* problem is feeding a variable (the field#) into the picker js so that when the close button is clicked it activates that field. I suspect it will require someone who either knows the script or is a Supreme Master Coder ;)

The "Wouldn't *THAT* be a monstrous mistake?" re. numbered script reps, had me head scratching. How else to do it?

Thanks for the help.

Old Pedant
Apr 6th, 2012, 01:03 AM
Well, maybe it wouldn't be. But it sure seems that way.

First of all, WHY would you use a different function for each different textarea?

And even if you do need a different function for different *types* of textareas (e.g., textareas used for different purposes), why would you then dynamically number the functions, instead of naming them based on the type/purpose of the textarea? (For that matter, how would you *create* dynamically numbered functions?? Oh, I know it *can* be done. But (a) why would you and (b) how would that make more sense than naming them based on type/purpose?)

So maybe "monstrous" mistake is a bit strong, but you'd need to convince me that it isn't at least some level of mistake.

arfa
Apr 6th, 2012, 02:43 PM
The editor command to get/set is FieldName.selection.getContent(); where FieldName is quite literal. I couldn't see how to feed in a variable to respond to each uniquely named textarea - ta1 ta2 ta3...
I was using a uniquely numbered function to resolve the original question.

They all have the same purpose - eg. superscript - but setting FieldName was the problem.
I have had success with xelawho's approach and can now have the link build as:
onclick=super($number); which feeds to a standalone.js:
function super(numb) { var name = 'field';
var number = numb;
window[name+number].selection.getContent(); }

There was no doubt in my mind that declaring essentially the same js over and again was not good practice. Which is why I asked the question.
So, level of mistake = my ignorance.

I really appreciate your engagement here. I started self-learning PHP at 55 and love it. JS is awesome but I can only manage a certain amount of brain massage at one time.

Thank you.

Old Pedant
Apr 7th, 2012, 12:25 AM
Ummm....if those are *REALLY* field *NAMES* then I think you may find your code only works in MSIE.

That is, if you have

<form id="myForm">
<textarea name="ta1"></textarea>
<textarea name="ta2"></textarea>
<textarea name="ta3"></textarea>
...

Only MSIE will recognize window["ta1"] as a reference to one of those textareas.

The more general solution would be


function super(numb) {
document.getElementById("myForm").elements["field"+numb].selection.getContent();
}

But there might well be much better ways. If you would show us the page live, maybe we could suggest them.

arfa
Apr 7th, 2012, 12:52 AM
Works fine in Firefox 3.5.5 (not the most recent).

I will try the form.elements approach.

thanks

Old Pedant
Apr 7th, 2012, 01:13 AM
If it works in FF (any version) it should work in all browsers.

But now you really have me curious as to just what exactly the HTML indeed looks like.

arfa
Apr 7th, 2012, 03:16 AM
I will try and find time to extract the essence.

My js tends to be a cobbling together of things I like to get what I require. In this case punyMCE (the tiny version is huge by comparison but has all the features) with jscolor and a few other snippets.

I will (hope to) PM you -OldP- a URL

A big thanks to xelawho for setting the direction in the first instance.