PDA

View Full Version : Setting up a list of VAR's



NGJ
05-25-2006, 01:50 PM
I'd like to store a list of text values (VAR's) that I can later refer to within my page.... to be used in something like:

<table>
<tr>
<td>Department: </td>
<td id="dept1"></td>
</tr>
</table>

... where dept1 has a preset value.

I understand HTML doesn't have variables, so I wondered how I could implement some sort of script to allow me to do so....?:p

Thanks in advance,
N.

jscheuer1
05-26-2006, 04:49 AM
In your example, 'dept1' is an id. It is what it is and cannot represent anything else other than its element that it identifies. To try and do so would most likely cause problems.

A list of variables is most commonly set up using the array object:


<table>
<tr>
<td>Department: </td>
<td id="dept1"></td>
</tr>
</table>

<script type="text/javascript">

var textStrings=new Array();
textStrings[0]="Hi There, ";
textStrings[1]="How are you";
textStrings[2]=" doing?";

document.getElementById('dept1').innerHTML=textStrings[0]+textStrings[1]+textStrings[2];
</script>

The above would do something but, it is unclear to me what you want to do. So, it is unlikely that this example does that. It may give you an idea of how to reach your goal.

NGJ
05-28-2006, 10:01 AM
Thanks John.
Now I have a simple working example of how to use an array - that's useful.

What I've been trying to do is to get an input from a user (eg. their name, dept., etc) and then be able to display this value multiple times within the page.

The problem seems to be that when using getElementById it only remembers the value the once. If you call it again, it displays a blank.
TWEY showed me how I can multiply this up to effectively set a number of identifiers to this value - within the one getElementById statement. This then allows me to refer to this value multiple times - but it does mean I have to be careful to create enough instances of it and keep track of it.

I was wondering if there is a simpler way of setting a value to something that can be referred to 'many' times? Using an array almost seems a bit heavy-handed as the item will only hold a single value - never a list of values. Regardless, multiple referrals result in blanks by this method also.

Is there any such direct replacement within HTML for what would be declaring a VAR in other languages? If not, then I guess I'll have to stick with lengthier getElementById statements. ;)

I appreciate you help.

N.

Twey
05-28-2006, 12:12 PM
There isn't, no. You can set a variable in ECMAScript then poll it for changes, but that is unnecessarily inefficient.

You must understand that HTML isn't a programming or scripting language; it's only a markup syntax.

jscheuer1
05-29-2006, 06:21 AM
This all sounds a bit more complex than it need to be but, I may be missing something.

You can have a text input that, when changed, will update any number of other values throughout the page, be these other inputs or simply text on the page somewhere. If they are other inputs that can also be edited by the user, changing any one of them can update the others. No variable storage is required other than in the values of the inputs/text strings themselves.

The code for this is a pretty basic combination of HTML and javascript but, if this is not what you need, I will not bother with an example at this time.

It would not be bulletproof for secure database use or work at all if javascript were disabled/unavailable.

NGJ
05-29-2006, 10:27 AM
Thanks John - I think what you're saying would work...

I'm using the Multi-Part Content script (featured in DD) to break up a large form - for more comfortable viewing on the screen.

I'd like to display the user's name (given by an input from the first page) at the top of each new part - just to personalise it.

Is there another way I can take this input value and then have it displayed multiple times? I was just going to set this up with multiple id's in a getElementById statement.... displaying each in turn on the top of each 'part'. If I could maybe have a "no name supplied" value that changes to the user's name (from their input on the first page) that would be a cleaner way of doing things...?
Would be grateful if you could demo code for this!:)

Thanks,

N.

jscheuer1
05-29-2006, 07:05 PM
OK, put this script in the head of your page:


<script type="text/javascript">
function initNames(el){
var txt=el.value;
var names=document.getElementsByName('username');
for (var i_tem = 0; i_tem < names.length; i_tem++)
if (names[i_tem].tagName.toLowerCase()=='a')
names[i_tem].innerHTML=txt;
else
names[i_tem].value=txt;
}
</script>

Then, wherever you want the username to appear have either this:


<a name="username">begining text</a>

or this:


<input name="username" type="text" value="Enter Your Name" onchange="initNames(this);">

NGJ
05-30-2006, 08:58 AM
Thank you John - that works great!!

I know this is going to be something I can re-use, so it's especially useful to me. :)

Cheers,

N.