944,201 Members | Top Members by Rank

Ad:
Feb 10th, 2007
0

Accessing objects that are not in the form fields

Expand Post »

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?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <body>
  3. <script language="javascript">
  4. function Function1() { document.TextBox.value = "You clicked the first button.";}
  5. function Function2() { document.TextBox.value = "You clicked the second button.";}
  6. </script>
  7. <input type="text" name="TextBox" value="This is the text box."><br>
  8. <input type="button" name="Button1" value="Button 1" onClick="Function1();">
  9. <input type="button" name="Button2" value="Button 2" onClick="Function2();">
  10. </body>
  11. </html>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
hkBattousai is offline Offline
33 posts
since Nov 2006
Feb 10th, 2007
0

Re: Accessing objects that are not in the form fields

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"  id="TextBox" name="TextBox"  value="This is the text box."><br>
        <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
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Improvement possible?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: search Keywords Email Alert





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC