Learning JavaScript--Cannot Solve Problem

When I run this code, I click submit on the form and nothing happens. The adding event handler part of the code, I got from a Lynda_com tutorial so I’m pretty sure it’s accurate, I’m just screwing up something basic I think. I’d appreciate any help.

<form type="POST" action="#">
<input id="userInput" type="text" size="60" /><br />
<input type="submit" id="submit" />
<script type="text/javascript">
function addEventHandler(objectNode, eventType, func, capture) {
	if (typeof (window.event) != "undefined")
		objectNode.attachEvent("on" + eventType, func);
		objectNode.addEventListener(eventType, func, capture);

function alertInput(objectNode) {
	addEventHandler(objectNode, "submit", regExpression(document.getElementById("userInput").value), false);

function regExpression(stringValue) {
	if (stringValue == null || stringValue.length == 0)
		alert(s	tringValue);

addEventHandler(window, "load", alertInput(document.getElementById("submit")), false);

Thank you!


I’m not sure exactly what you are trying to do. Form validation using javascript or AJAX submission?

You can try this:

<form type="POST" action="/your_url" onsubmit="javascript_function_name();">

Or something similar.

Start with correcting the syntax.

s tringValue should be stringValue

Fixed this and tried the other solution, I’m still having the problem. No alert dialog comes up in either case.

What is supposed to happen, and where is the tutorial that you followed located?

Lynda.com Practical and Effective JavaScript video series.

I’m simply exploring with JavaScript. With the above code, it’s only supposed to send an alert message. I’m getting no alert message. Even with the following code, no alert message:

<form type="POST" action="#" id="form" onSubmit="alertInput(document.getElementById("form")">
<input id="userInput" type="text" size="60" /><br />
<input type="submit"/>
function alertInput(objectNode) {
	//addEventHandler(objectNode, "submit", regExpression(document.getElementById("userInput").value), false);

Well, that course has Exercise Files that are copies of the identical files that Lynda uses in her tutorial.

As Lynda’s training material is behind a pay-wall, which I suspect that you have access to as you are taking her tutorial, I suggest that you obtain those exercise files yourself, try them out to confirm that they work, and then compare the working one with your non-working one.

Alright I’ll check again. I have the Excercise Files.

I’m just surprised that my code in the second case didn’t work. I’m assuming it’s fairly basic… the event handler actually in the HTML connecting to one function which simply says alert(“test”);.

Edit: Even weirder, the Exercise Files work in IE and FireFox, but not in Google Chrome (and it’s running JavaScript). Does Google Chrome handle JavaScript differently?

There are a couple of differences that can be made to make it work. I’ll go over the parts that you need to pay closer attention to, so that you can go back over the tutorial and rediscover what should be there.

First, is to assign a function to the submit event. Currently the regExpression function is being run directly. That needs to be contained within an anonymous function instead.

Second, is during the load event where you pass an object to alertInput. You don’t want to pass it the submit button itself to alertInput, because the submit button doesn’t have a submit event. Instead, you want to pass a reference to the form itself.

Go back over those and see what more you can learn about them from the tutorial.

Thank you very much for the explanation

Please don’t bump threads.

Forum Guidelines

In fact, doing so hurts your chances of getting help, since the regulars won’t find you on the “unanswered threads” page. Thanks :slight_smile:

And why the bump I question.

He has the sample files from which he can obtain the original working code.
He has the tutorial videos that teach him how to create the working code.
He has the explanations of what needs to be done to create working code.

I am not going to explicitly write the working code.

In this situation he will learn more from the act of studying the sample code and the video tutorial more closely, in concert with the provided advice, so that he can find and learn about the issues at hand.

Looks like you are missing a closing parethesis:

<form type=“POST” action=“#” id=“form” onSubmit=“alertInput(document.getElementById(“form”))”>

Not only that, but you are not properly using quotes.


The second double quote terminates the string. You’ve got to use single quotes inside strings with double quotes or escape them with \ .