Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying a variable's name, not its assigned value... 1

Status
Not open for further replies.

sonicsnail

Technical User
Nov 10, 2001
48
0
0
IN
Hi. I'm creating a phone-number page for text-messaging from.

I have

var JohnSmith = "0123456789,";
var PeteJones = "0223456789,";
var PaulSmith = "0333456789,";
var AllContacts = John + Pete + Paul;

etc etc..

I have a function that fires the numbers into my textarea.. (sometimes I have to put quite a lot of the contacts in at one time, so I can have

onClick="putNumber(John);"
onClick="putNumber(Pete);"
or even
onClick="putNumber(AllContacts);" (I have the commas in the variables already!)

---Here's my Problem ----
I now want to have a 2nd function that adds the NAMES (not their assigned numbers to ANOTHER text area... so I can visually confirm who's numbers have been added to the textarea.

Ideally I'd like to put something like

onClick="PutName(John);"

BUT - of course it'll put John's number, not his name. I suppose I could do it as PutName("John"); but that falls over when I put PutName("AllContacts").

Hopefully someone can give some advice on how to get round this...

thanks,

Pete.
 
Try this code :

<script>
<!--
var pauln = '07923456781';
var paul = ' Paul';
var davidn = '05622456751';
var david = ' David';
var johnn = '56233456782';
var john = ' John';
var fredn = '98923423745';
var fred = ' Fred';
var allname = paul + david + john + fred;
var allnum = pauln + davidn + johnn + fredn;

function addnamenum(number, name) {
document.myForm.numbers.value = document.myForm.numbers.value + number + &quot;,&quot;;
document.myForm.names.value = document.myForm.names.value + name + &quot;,&quot;;
}
-->
</script>

<form name=&quot;myForm&quot;>
Numbers :<br><textarea name=&quot;numbers&quot; rows=&quot;10&quot;></textarea>
<br>
Names :<br><textarea name=&quot;names&quot; rows=&quot;10&quot;></textarea><br>
<input type=&quot;button&quot; value=&quot;Add Paul&quot; onclick=&quot;addnamenum(pauln, paul)&quot;><br><br>
<input type=&quot;button&quot; value=&quot;Add David&quot; onclick=&quot;addnamenum(davidn, david)&quot;><br><br>
<input type=&quot;button&quot; value=&quot;Add John&quot; onclick=&quot;addnamenum(johnn, john)&quot;><br><br>
<input type=&quot;button&quot; value=&quot;Add Fred&quot; onclick=&quot;addnamenum(fredn, fred)&quot;><br><br>
<input type=&quot;button&quot; value=&quot;Add all names and numbers&quot; onclick=&quot;addnamenum(allnum, allname)&quot;><br><br><br>
<input type=&quot;reset&quot; value=&quot;Clear names and numbers&quot;>
</form> Regards

Big Dave


** If I am wrong I am sorry, but i'm only trying to help!! **

 
Here is a very simple example of how to do this in a more programmatic fashion. I have also started a &quot;person&quot; object, so that you can turn this into a completely Object-Oriented library, if you want to (Read &quot;Javascript Objects&quot; by Wrox press):

Code:
<html>
<head>

<script Language=&quot;Javascript&quot;>
var people = new Object();
  people.names = Array(&quot;John&quot;, &quot;Peter&quot;, &quot;Paul&quot;);
  people.numbers = Array(&quot;168496543&quot;, &quot;987316862&quot;, &quot;168431353&quot;);
</script>

</head>
<body>

<script Language=&quot;Javascript&quot;>
for(i = 0; i < 3; i++){
 document.write(people.names[i]);
 document.write(&quot;&nbsp;&nbsp;&nbsp;&nbsp;&quot;);
 document.writeln(people.numbers[i] + &quot;<br>&quot;);
}
 allnames = people.names.join(&quot;, &quot;);
 document.write(allnames);
</script>

</body>
</html>

So you see how, working with arrays, you can process this kind of information much more easily. This is just a simple example to get you started. Javascript has some very good array manipulation features, so you should be able to extract and manipulate any information you want. If you really want to get into a true O-O approach to doing this, let me know and we will pursue this example a little further.
 
Addendum: that empty space in the second document.write line was actually a series of &#38;nbsp; characters, but the forum software changes things after a preview of your post.
 
Yeah,
I know you should do it with arrays, I was just giving a simple example. Regards

Big Dave


** If I am wrong I am sorry, but i'm only trying to help!! **

 
Davidbyng - first off, thanks..

I read your post before Rycamor had posted, and have implemented your functions with success. I didn't realise I could have multiple things in my

function fnName(here,andhere);

(forgot to mention my JS newbie status.. things are actually starting to make sense now at last - I've been copy/pasting JS for years!)

Rycamor - thanks to you too. - I *knew* arrays were what I needed.. I've been searching online for 2 days now for ideas of how to implement this - and I kept finding arrays everywhere. Though I couldn't work out how to do it. However your post doesn't make clear how the array works with the form...

I'd appreciate a little more info on it..

(I'll even make it more fun :) - what about an array like..

Pete,0123456789,group1
John,0325642029,group2
Paul,0200235789,group1
Chris,0894645613,group2

so I can make a function that finds all the group2's (for example) and fires their number into my text area? (its the &quot;finds all the group2s&quot; parts thats been tripping me up)

thanks again to both of you.

Pete.
 
try something like this (sorry, my JS is a little rusty, watch out for syntax errors...):

people=new Array()

function person(name,number,group)
{
this.name=name
this.number=number
this.group=group
}

function addPerson(name,number,group)
{
var pers=new person(name,number,group)
people[people.length]=pers
}

then you can construct loops that go through the people array and extract the group name, find if it exists already and then do something or other with your person data. Once you have built the people array and populated it with persons then the only issue is how best to sort your data.


Hope that is helpful. &quot;Lets integrate!&quot;
If it doesn't work it's not my fault.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top