954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Accessing objects that are not in the form fields

[IMG]http://www.contentmart.com/ContentMart/Create/images/dom.gif[/IMG]
I want to ask if there is a way to access an obcect which is not in a form field. My textbook doesn't cover that topic. All the sample code in it is done with the form fields.
The picture above illustrates the DOM object model that is used in JavaScript. What I understand from the picture is there is no way to access a control which is not in a form field.

For example, is there any way to make the code below work?

<html>
    <body>
        <script language="javascript">
            function Function1() { document.TextBox.value = "You clicked the first button.";}
            function Function2() { document.TextBox.value = "You clicked the second button.";}
        </script>
        <input type="text"  name="TextBox"  value="This is the text box.">
        <input type="button" name="Button1" value="Button 1" onClick="Function1();">
        <input type="button" name="Button2" value="Button 2" onClick="Function2();">
    </body>
</html>
hkBattousai
Light Poster
33 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

The easiest way is to identify the inputs rather than name them:

<html>
    <body>
        <script language="javascript">
            function Function1() { document.getElementById('TextBox').value = "You clicked the first button.";}
            function Function2() { document.getElementById('TextBox').value = "You clicked the second button.";}
        </script>
        <input type="text"  <strong>id="TextBox"</strong> name="TextBox"  value="This is the text box.">
        <input type="button" name="Button1" value="Button 1" onClick="Function1();">
        <input type="button" name="Button2" value="Button 2" onClick="Function2();">
    </body>
</html>


Those 'id' attributes have got to be unique.

There are other ways to get at objects that aren't id'ed; see this page: http://www.pageresource.com/dhtml/ryan/part4-2.html

MattEvans
Veteran Poster
Moderator
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You