SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Weid behaviour calling value of a selfmade JS class

    Hi,

    I experienced some weird behaviour when alerting the value of a selfmade JS class.

    I made the class myTestobj with one property called 'value'. The object 'mytest' is of that type. If I alert 'mytest.value' before the formtag it says 'Normal behaviour' as expected. But when I alert it inside the form it says 'Not so normal behaviour!'.

    As you can see I have an element with the name 'mytest' in the form.
    Why does it alert the value of this element when I call it inside the form and not the value of my object?


    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">
    <head>
      <title>Weird behaviour</title>	
      <script type="text/javascript">
        function myTestobj(){
          this.value = 'Normal behaviour';
        }
    				
        var mytest = new myTestobj();
      </script>
     </head>
      
      <body>
        <input type="button" onclick="alert(mytest.value);" value="Before the form">
        <form>
          <input type="button" onclick="alert(mytest.value);" value="In the form">
            <table>
    	  <tr>
    	    <td><input type="text" name="mytest" value="Not so normal behaviour!" /></td>
    	  </tr>
    	</table>
        </form>
      </body>
    </html>

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Go through your js and your html and write down every variable name you use and every name you assigned to a name attribute. Create columns for each name, and put tick marks under the column headings every time you use a name. Also add an extra tick mark under "value" since js uses that name as well.

  3. #3
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does that because inside the form the the form control named "mytest" gets reached before the global variable named "mytest" when the parser tries to resolve the reference.

    To get around that, you can specify the object to which the variable is attached. Global variables get attached to window, so you can refer to it as window.mytest.value.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  4. #4
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah thank you Kravvitz, that makes a lot of sense!

    Not so weird behaviour after all.


Bookmarks

Posting Permissions

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