I hate JavaScript

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Why do I always get undefined in the alert box? I just want value assigned to the value of the text box. It should be so simple...

Code in action, source

<html>
<head>
&lt;script type="text/javascript"&gt;
function validate()
{
value = document.regform.regex.value;
alert(value)
}
&lt;/script&gt;

</head>
<body>
<form name="regform">
<fieldset id="regex">
<legend>Regular Expression</legend>
<input type="text" name="regex" onkeyup="validate()"/>
</fieldset>
</form>

</body>
</html>

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

It works for me in Konqueror but not in Firefox. The reason is that they have different ideas of what document.regform.regex means. Konqueror thinks it's the input field (as you do) but Firefox thinks it's the fieldset named regex. So document.regform.regex[1].value should work instead. But it may break in other browsers.

Fyi, what you hate is not Javascript but rather the DOM. And you probably hate the DOM because you're not using it correctly. What you should be using is something like the following:

<html>
<head>
&lt;script type="text/javascript"&gt;
function validate()
{
        value = document.getElementById("regex").value
        alert(value)
}
&lt;/script&gt;

</head>
<body>
<form name="regform">  
        <fieldset id="regexset">
                <legend>Regular Expression</legend>
                <input type="text" id="regex" name="regex" onkeyup="validate()"/>
        </fieldset>
</form>

</body>
</html>

Smiling

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Thanks, the getElementById thing works!

Quote: Fyi, what you hate is not Javascript but rather the DOM. And you probably hate the DOM because you're not using it correctly. What you should be using is something like the following:

Why is document.getElementById better than just going stepping through the elements on the page? Is it because the browsers have different ideas on what certain elements are and getElementById works right every time? Is it faster?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

teammatt3;226298 wrote: Why is document.getElementById better than just going stepping through the elements on the page? Is it because the browsers have different ideas on what certain elements are and getElementById works right every time? Is it faster?

It's similar to HTML standards. The Document Object Model 2 Core is a standard produced by the W3C, specifying [incode]getElementById[/incode] among other things. This should work in exactly the same way everywhere (and probably does). There is no guarantee of it being faster or slower.

The code you were using, however, is non-standard, and was the traditional way of doing things in the absence of a compliant DOM implementation. Its behaviour naturally varies with every user agent.

Unfortunately, there is a lot of existing documentation and code that doesn't use the recommended DOM standard, so you're quite likely to be misinformed if you look at outdated sources.

Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.