Accessing objects that are not in the form fields

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Nov 2006
Posts: 21
Reputation: hkBattousai is an unknown quantity at this point 
Solved Threads: 0
hkBattousai's Avatar
hkBattousai hkBattousai is offline Offline
Newbie Poster

Accessing objects that are not in the form fields

 
0
  #1
Feb 10th, 2007

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>
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Accessing objects that are not in the form fields

 
0
  #2
Feb 10th, 2007
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
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC