Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Friday October 21, 2005
05:31 PM

Inigo Montoya: Javascript Guru

[ #27276 ]

Inigo Montoya: "You keep using that JavaScript. I do not think it means what you think it means."

<html>
  <head>
    <script type="text/javascript">
      function test_me(search) {
        search['age'].value = 21;

        for (var i in search.elements) {
          var elem = search[i];
          if ('age' == elem.name) {
            alert("From form: " + search['age'].value);
            alert("From elem: " + elem.value);
            break;
          }
        }
      }
    </script>
  </head>
  <body>
    <form name="search" id="search" action="." onsubmit="test_me(this); return false">
      <input type="text" name="age" value="18"/>
      <input type="text" name="age" value="19"/>
      <input type="text" name="one_value" value=""/>
      <input type="submit" value="Test it!"/>
    </form>
  </body>
</html>

That should pop up two alerts. The first says the value is 21. The second alert says the value is 18. That took me quite a while to debug.

In other news, Javascript is a neat language. It's debugging facilities, however, are even worse than Perl's.

By the way, how do I tell if a given element is an array (as in "age" above) or a single value (as in "one_value" above)? I can't seem to figure that out. I can get it working if I try to use the darned thing as an array and then catch the exception when it fails, but that's awful :(

Update: the problem is solved with this function:

      function elemIsArray(elem) {
        var string = Object.prototype.toString.apply(elem);
        var name = string.substring(8, string.length - 1);
        return (name.match(/nodelist/i) != null);
      }

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.