www.webdeveloper.com
Results 1 to 9 of 9

Thread: Dynamic Object Names

  1. #1
    Join Date
    May 2012
    Posts
    5

    Dynamic Object Names

    I couldn't find the answer to this in my O'Reillys book, so I thought I'd ask here.

    I have a collection of text fields arranged as a table. The contents of the fields will be calculated by a formula and held in an array variable.

    I'd like to be able to copy the contents of the array into the collection of fields.

    The fields are named CellXY where 'X' is the X-dimension and 'Y' is the Y-dimension. So, Cell11, Cell42, Cell79, etc.

    What I'd like to do is run a couple of loops to run through the dimensions of the array. So, as each value in the array is touched, I can set the value of the cell to that spot in the array. Basically something like, 'Cell'+'X'+'Y'+'.value' = Array(X,Y)

    I realize that this is not actual code, but I'm hoping it gets the idea of what I want across. Is this at all possible?

  2. #2
    Join Date
    Nov 2010
    Posts
    1,084
    seems simple enough. can you show us the code you have so far so we don't have to reinvent the wheel?

  3. #3
    Join Date
    May 2012
    Posts
    5
    Well, at the moment, I don't have any code written. This is to be a function and all I have are the objects set up the way I want them to look. But, that is at home and I am at work.

    If I were doing this in VB6, there'd be no problem, as grouped controls have an index property I can manipulate, but I am a vast newbie in JavaScript and have not seen anything comparable.

  4. #4
    Join Date
    Nov 2010
    Posts
    1,084
    it's not really a big deal in javascript, either - in fact there are various ways of approaching the problem. when you have a chance, post your html.

    something I don't get, though - you have one set of fields, then you get the values from those fields, do something to them, store those values in an array and then output the array values to another set of fields? or am I confused?

  5. #5
    Join Date
    May 2012
    Posts
    5
    Quote Originally Posted by xelawho View Post
    it's not really a big deal in javascript, either - in fact there are various ways of approaching the problem. when you have a chance, post your html.
    Unfortunately, it's not in html. It's a form created by a 3rd-party design tool that is very basic. The JavaScript is either a single line associated with a form field, or a function asssigned to the form itself that can be referenced.

    something I don't get, though - you have one set of fields, then you get the values from those fields, do something to them, store those values in an array and then output the array values to another set of fields? or am I confused?
    What I have is a 9x7 array variable that is initialized to zero.

    Then, I drop some number into one of the cells.

    Then, for each orthagonal cell, I subtract 1, repeating until all cells are filled with the new value. The formula is;
    k - (|x-a| + |y - b|) where;
    k: the dropped number
    a,b: the current array coordinate
    x,y: the starting array location

    Once the array is finished, I need to dump the value of each cell into a corresponding text box among the boxes arranged into a table.

    In VB6, I'd just have two loops, one to loop through the columns and one to loop through the rows with a counter to increment the index value for the grouped text boxes. Or, there's probably a table field I could use that would reference x, y coordinates. But, anyway, there is a dynamic property of the text boxes (or table) that I can manipulate via code.

    If I could reference a text box dynamically in JavaScript, that'd fix me up. I'd just name the boxes with the x, y coordinates as part of the name, run through the loops and shove the appropriate array value into the .value property of each dynamically referenced text box.

    Although, if there's a way to create a table in JavaScript that'd be better. But, I don't know of one, except - I believe - via Ajax, which I can't use in this case. Well, that I know of.

  6. #6
    Join Date
    Nov 2010
    Posts
    1,084
    here is a good example for creating tables via javascript - no ajax required

    your page must generate html at some point for it to be seen - just hit view source after the page loads, it should all be there.

    assuming that your form is the first form on the page you can dynamically set the field values like this:
    Code:
    document.forms[0]["cell"+x+y].value="something"
    document.forms[0]["cell"+y+x].value="something else"

  7. #7
    Join Date
    May 2012
    Posts
    5
    Quote Originally Posted by xelawho View Post
    here is a good example for creating tables via javascript - no ajax required
    I'll have to take some time to digest that one...

    your page must generate html at some point for it to be seen
    Actually, no. At least, not that I can see.

    The form will be viewed in a viewer designed to show the form made with the designer tool. No browser is used. It might be able to export to html, but I don't know for certain.

    assuming that your form is the first form on the page you can dynamically set the field values like this:
    Code:
    document.forms[0]["cell"+x+y].value="something"
    document.forms[0]["cell"+y+x].value="something else"
    Ah!

    This looks like something that is along the lines of what I'm looking for. I'll have to try it out tonight.

    Many thanx!

  8. #8
    Join Date
    Nov 2010
    Posts
    1,084
    you'íre welcome. although it sounds like you might have trouble referencing the form if it does not exist in the DOM - give it a try anyway and post back if there are problems

  9. #9
    Join Date
    May 2012
    Posts
    5
    I haven't had a chance to try out your document suggestion, xelawho, but I realized that I don't need to create any sort of loop.

    As I was creating the cell objects on the form, it hit me that I could make a single function to get the value for a particular cell based upon its x,y coordinates that I can pass to the function. So, I just made that function, plopped it into the source field for the first cell with the coordinates hard coded and just C&Pd the object for the rest of the cells, changing the passed coordinates as I pasted each one.

    It woiks!

    But, I do still need to know if objects can be dynamically referenced, so I'll still have to study your suggestion, because it may hold the answer - or, at least, the key - to solving that issue.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles