Results 1 to 8 of 8

Thread: Setting up a list of VAR's

  1. #1
    Join Date
    Oct 2005
    Location
    Liverpool, UK
    Posts
    87
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Setting up a list of VAR's

    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....?

    Thanks in advance,
    N.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Oct 2005
    Location
    Liverpool, UK
    Posts
    87
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question

    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.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Last edited by Twey; 05-28-2006 at 12:27 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Oct 2005
    Location
    Liverpool, UK
    Posts
    87
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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.

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, put this script in the head of your page:

    Code:
    <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:

    HTML Code:
    <a name="username">begining text</a>
    or this:

    HTML Code:
    <input name="username" type="text" value="Enter Your Name" onchange="initNames(this);">
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Oct 2005
    Location
    Liverpool, UK
    Posts
    87
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •