Results 1 to 6 of 6

Thread: wheres tham thar elements ?!?!?

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Talking wheres tham thar elements ?!?!?

    Code:
    Function CalledOn(elementTxtBox)
    {
    	var paramElementTxtBox = form.elements.item(elementTxtBox);
    	alert(elementTxtBox.value)
    }
    
    FunctionProper() {
    	CalledOn(document.form.item("text1"))		
    }
    I want to try & pass an element into a function, and call on it's properties & methods (bit like vb's Controls collection). Apparently this is possible in Javascript, using the name of the element rather than the element number/index.

    Anyone got any ideas how I can do this one please ?!?!?!?!
    Cheers!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    It's easy to declare an object with properties in JavaScript--you don't. Instead, you just start using the properties in your code:

    elementTxtBox.name = "myControl";
    elementTxtBox.value = "Yes";

    Unfortunately, I can't help you with defining methods for elementTxtBox.

    cudabean

  3. #3
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    Oops, made a mistake. It's not quite that easy:

    elementTxtBox.name = new Object();
    elementTxtBox.name = "myControl";
    elementTxtBox.value = "Yes";

    cudabean

  4. #4
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    Grrr. This is finally correct:

    elementTxtBox = new Object();
    elementTxtBox.name = "myControl";
    elementTxtBox.value = "Yes";

  5. #5

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Thanks cudabean,

    If I understand this right though, you're defining a new element on the page?
    My idea (if it's possible) is to use an existing one. I could use :
    Code:
    FunctionProper() {
    	alert(document.forms.elements.item("text1"))		
    }
    In the actual function, but if I had 5 function doing the same thing, I was hoping to make a general function with the messagebox/alert code in, and just call this from the 5 functions I need, passing in the name of the element/textbox I want to display the value of.

    Not sure if that one made too much sense there, sorry.
    Thanks!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    I defined a new object that doesn't relate to anything on the page. I think I misunderstood your original premise. The object I created is only used to store data in a structure.

    In looking back at your original message I think I understand better.

    I think your original code is close to correct, except that I don't think that
    document.forms.elements.item("text1") works.
    document.forms.elements.item(1) works though.

    document.forms[0].elements["text1"] should work.
    document.form.text1 should work too.

    HTH

    cudabean

Posting Permissions

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



Click Here to Expand Forum to Full Width