Text Validation Not Working

Alrighty then!

I’m trying to create a simple form. The user enters a string of text, presses a button, and the JavaScript validation script does its magic, then an alert pops-up on screen to tell the user they’re correct. It isn’t working. I’ve tried various alternatives with names, ids, changing stuff around and so on. Yet, I also have an age validation script and that works fine, I’ve tried making the text validation into an age validation, but it doesn’t work. So it must be something with the form…? I’ve added both the working age validation and the not-currently-working text validation. Please, if you can (and I know you can!), help me. I’ve got Head First JavaScript, DOM Scripting, Simply JavaScript, and JavaScript: The Good Parts sprawled on my bed and I’ve been through them with nothing to help me. Otherwise, I’ve missed a page or something!

Text Validation, HTML (quick-question.html)

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Quick Question</title>
		<link rel="stylesheet" media="screen" href="quick-question.css">
	</head>

	<body>
		<div class="quick-question">
			<p class="header">Quick Question</p>

			<p class="question">Who invented the World Wide Web?</p>

			<form name="question-1" method="post" action="#">
				<label for="answer">Answer:</label>
				<input type="text" value="" id="answer-1" name="answer-1" class="required" />
				<input type="submit" value="Submit" id="btnSubmit" name="btnSubmit" />
			</form>
		</div>

		<script src="quick-question.js"></script>
	</body>
</html>

Text Validation, JavaScript (quick-question.js)

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function fnAnswers() {
	if (!document.getElementById) return false;

	// Answer Area

	var answerArea = document.question-1.answer-1.value;

	// Answer Variables

	var ansOne = "Tim Berners-Lee";

	// Question & Answer Logic

	if (answerArea == ansOne) {
		alert("Correct!");
	}
}

function fnSubmitAnswer() {
	if (!document.getElementById) return false;
	var btnSubmit = document.getElementById("btnSubmit");
	btnSubmit.onclick = fnAnswers;
}

addLoadEvent(fnSubmitAnswer);

And, as I said, the working age validation:

Age Validation, HTML (age-checker.html)

<!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">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Age Checker</title>
		<link rel="stylesheet" type="text/css" media="screen" href="stylesheet.css" />
		<script type="text/javascript" src="age-checker.js"></script>
	</head>

	<body>
		<div id="wrapper">
			<h1>Age Checker</h1>

			<p>Type your age in the form field below. You will receive a different popup alert depending
			on your age. If you are younger than 18 years old then you will be warned however, if you are
			 older than 18 then you will be welcomed. Try it!</p>

			<form name="myform">
				<input type="text" name="myagefield" id="myagefield" value="" />
				<input type="button" name="btnClick" id="btnClick" value="Click" />
			</form>
		</div>
	</body>
</html>

Age Validation, JavaScript (age-checker.js)

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function fnCheckAge() {
	if (!document.getElementById) return false;
	var myAge = document.myform.myagefield.value;
	
	if (myAge > 18)
		{
			alert("Welcome.");
		}
	else
		{
			alert("Sorry you are too young.");
		}
}

function fnClickCheck() {
	if (!document.getElementById) return false;
	var btnClick = document.getElementById("btnClick");
	btnClick.onclick = fnCheckAge;
}

addLoadEvent(fnClickCheck);

So, any takers?

var answerArea = document.question-1.answer-1.value;

Think about what that is saying din the JavaScript Andrew.
question minus one dot answer minus 1

Change the form input id to use underscores NOT minus signs :slight_smile:
And the JavaScript to:

var answerArea = document.question_1.answer_1.value;

Rename the *.js file as well. Never use hyphens; use underscores as spike2 suggests or capitalization and concatenation (question1, questionOne)

What am I supposed to say to that Mike? Why is it always such a simple solution that manages to evade me for such a long time? I didn’t even think to try that. Hyphens are fine with HTML and CSS, I assumed they were fine with JavaScript too, but, they obviously aren’t!

You know, it almost makes me want to cry a tiny little bit. But thank you. At over 100 views and 0 replies I was beginning to give up hope, and then you come in like a Knight in shining armour. It works :slight_smile: Now to actually script it how it should be want it to be.

You didn’t REALLY think I was going to use the alert method in an actual website did you?! :stuck_out_tongue:

It’s fine with hyphens in the name of the file. Why is it that I should use underscores or capitalization for the file name too?

Hi,

It’s fine with hyphens in the name of the file. Why is it that I should use underscores or capitalization for the file name too?

It makes for consistency. If you avoid hyphens everywhere, you don’t need to remember whether languages permit them or not. Then again, that’s just the way I see and do things. :slight_smile:

You’re welcome Andrew :slight_smile:

I guess its all a matter of preference but I lean towards Vincent’s way of thinking with the hyphens.

Ah, yes, that’s a fair point to make. I understand your point, and I agree with it completely. But I don’t actually call the scripts on live sites anything with hyphens, it’ll usually just be scripts.js or script.js. I’ve decided to go for camelCasing i.e. questionOne, ansOne, etc.

Now, this thread isn’t over yet :stuck_out_tongue: I’ve come across another hiccup. I already came across it after replying to this thread, but I wanted to give it a few more tries (and days) before I asked for help again, and, well, I’m here again, because I’ve failed to fix the issue I’m having.

I’ll post the HTML first and then the JS, you’ll be able to see what I’m trying to do from the code. Basically, if the person enters in the form field “Tim Berners-Lee” then it’ll alert the user with the message “Correct!” but this isn’t working. What I actually want is to do an else after that for a “Wrong!” alert but I thought I’d try to get the first alert working first before the second…But actually, let’s do the two birds with one stone.

HTML File NOT WORKING - quick-question-alert.html

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Quick Question</title>
		<link rel="stylesheet" media="screen" href="quick-question.css">
	</head>

	<body>
		<div class="quick-question">
			<p class="header">Quick Question</p>

			<p class="question">Who invented the World Wide Web?</p>

			<form name="questionOne" method="post" action="#">
				<label for="answer">Answer:</label>
				<input type="text" value="" id="answerOne" name="answerOne" class="required" />
				<input type="submit" value="Submit" id="btnSubmit" name="btnSubmit" />
				<div class="answerPlacement"></div>
			</form>
		</div>

		<script src="quick-question.js"></script>
	</body>
</html>

And the JavaScript File NOT WORKING - quick-question-alert.js

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function fnAnswers() {
	if (!document.getElementById) return false;

	// Answer Area

	var answerArea = document.questionOne.answerOne.value;

	// Answer Variables

	var ansOne = "Tim Berners-Lee";

	// Question & Answer Logic

	if (answerArea == ansOne) {
		alert("Correct!");
	}
	else {
		alert("Wrong!");
	}
}

function fnSubmitAnswer() {
	if (!document.getElementById) return false;
	var btnSubmit = document.getElementById("btnSubmit");
	btnSubmit.onclick = fnAnswers;
}

addLoadEvent(fnSubmitAnswer);

Now, I do have another similar script that works too, so I’ll stick this in here just to show you that I can get alerts to work and I can do at least a little bit of basic JavaScript haha. I mean, seriously, I know this is really basic JavaScript, but I don’t know why the hell it isn’t working again. It’s really frustrating me that I know this is really basic but I can’t get it to work.

HTML File WORKING - age-checker.html

<!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">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Age Checker</title>
		<link rel="stylesheet" type="text/css" media="screen" href="stylesheet.css" />
		<script type="text/javascript" src="age-checker.js"></script>
	</head>

	<body>
		<div id="wrapper">
			<h1>Age Checker</h1>

			<p>Type your age in the form field below. You will receive a different popup alert depending 
			on your age. If you are younger than 18 years old then you will be warned however, if you are
			 older than 18 then you will be welcomed. Try it!</p>

			<form name="myform">
				<input type="text" name="myagefield" id="myagefield" value="" />
				<input type="button" name="btnClick" id="btnClick" value="Click" />
			</form>
		</div>
	</body>
</html>

JavaScript File WORKING - age-checker.js

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function fnCheckAge() {
	if (!document.getElementById) return false;
	var myAge = document.myform.myagefield.value;
	
	if (myAge > 18)
		{
			alert("Welcome.");
		}
	else
		{
			alert("Sorry you are too young.");
		}
}

function fnClickCheck() {
	if (!document.getElementById) return false;
	var btnClick = document.getElementById("btnClick");
	btnClick.onclick = fnCheckAge;
}

addLoadEvent(fnClickCheck);

So, what did I do wrong this time?

Woopsy daisy. The HTML file references at the bottom <script src=“quick-question.js”></script> but the name of the JavaScript file is quick-question-alert.js…I amended it, and it now works perfectly. Ugh. How embarrassing! Such a careless mistake!

At the same time, how was there 20 new views on this thread and not a single person spotted my mistake?! :eek: Tsk tsk!

Anyway, I do still have another problem with it, which I won’t post yet until I’ve tried sorting it out myself, but I just wanted to update the thread to inform everyone of my mistake and that it has been fixed and now works.

Ok, so, I’ve tried, and failed. The conditional statement works, but I of course don’t want to use alerts. I’m trying to insert text using a script, and when I click the Submit button it does insert the text of “Correct!” and “Wrong!”…But only for a flash of a second, then it disappears. But I want it to stay there. Again, here’s the code for both files.

HTML File - quick-question.html

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Quick Question</title>
		<link rel="stylesheet" media="screen" href="quick-question.css">
	</head>

	<body>
		<div class="quick-question">
			<p class="header">Quick Question</p>

			<p class="question">Who invented the World Wide Web?</p>

			<form name="questionOne" method="post" action="#">
				<label for="answer">Answer:</label>
				<input type="text" value="" id="answerOne" name="answerOne" class="required" />
				<input type="submit" value="Submit" id="btnSubmit" name="btnSubmit" />
				<div id="answerPlacement"></div>
			</form>

			<p>Lol</p>
		</div>

		<script src="quick-question.js"></script>
	</body>
</html>

JavaScript File - quick-question.js

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function fnAnswers() {
	if (!document.getElementById) return false;

	// Answer Response

	function fnAnswerResponseCorrect() {
		var answerParagraph = document.createElement("p");
		var answerPlacement = document.getElementById("answerPlacement");
		answerPlacement.appendChild(answerParagraph);
		var answerText = document.createTextNode("Correct");
		answerParagraph.appendChild(answerText);
	}

	function fnAnswerResponseWrong() {
		var answerParagraph = document.createElement("p");
		var answerPlacement = document.getElementById("answerPlacement");
		answerPlacement.appendChild(answerParagraph);
		var answerText = document.createTextNode("Wrong");
		answerParagraph.appendChild(answerText);
	}

	// Question & Answer One

	if (document.questionOne.answerOne.value == "Tim Berners-Lee") {
		fnAnswerResponseCorrect();
	}
	else {
		fnAnswerResponseWrong();
	}
}

function fnSubmitAnswer() {
	if (!document.getElementById) return false;
	var btnSubmit = document.getElementById("btnSubmit");
	btnSubmit.onclick = fnAnswers;
}

addLoadEvent(fnSubmitAnswer);

So, any ideas anyone?

This particular piece of code:

var answerParagraph = document.createElement("p");
var answerPlacement = document.getElementById("answerPlacement");
answerPlacement.appendChild(answerParagraph);
var answerText = document.createTextNode("Wrong");
answerParagraph.appendChild(answerText);

Does work on its own. It’s from page 130 of DOM Scripting by Jeremy Keith.

You have a scope problem by nesting multiple functions, and calling the function that is within another. Could be wrong.

You could just do something like:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Quick Question</title>
		<link rel="stylesheet" media="screen" href="quick-question.css">
<script>
	function fnAnswer() {
		var result = '';
		var answerParagraph = document.createElement("p");
		var answerPlacement = document.getElementById("answerPlacement");
		answerPlacement.appendChild(answerParagraph);
		/* answers */
		if (document.questionOne.answerOne.value == "Tim Berners-Lee") {
			result = "Correct";
		} else {
			result = 'Narf!';
		}
		
		var answerText = answerPlacement.innerHTML = result;
	}
</script>
	</head>


	<body>
		<div class="quick-question">
			<p class="header">Quick Question</p>


			<p class="question">Who invented the World Wide Web?</p>


			<form name="questionOne" onSubmit="return false;">
				<label for="answer">Answer:</label>
				<input type="text" value="" id="answerOne" name="answerOne" class="required" />
				<input type="submit" value="Submit" id="btnSubmit" name="btnSubmit" onclick="fnAnswer();" />
				<div id="answerPlacement"></div>
			</form>


			<p>Lol</p>
		</div>


	</body>
</html>

Yes, I could, and that actually works! :smiley: But there are two problems with that, for me at least. That solution uses innerHTML which isn’t part of the JavaScript standard, and I want to keep to the standards, which is why I’m trying to get the standards based version to work that Jeremy Keith uses in his DOM Scripting book. Also, when I try to implement that externally it gives me the same result as my existing script, it appears for a flash of a second then disappears.

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function fnAnswers() {
	if (!document.getElementById) return false;

	// Answer Response

	var result = '';
	var answerParagraph = document.createElement("p");
	var answerPlacement = document.getElementById("answerPlacement");
	answerPlacement.appendChild(answerParagraph);

	// Question & Answer One

	if (document.questionOne.answerOne.value == "Tim Berners-Lee") {
		result = "Correct!";
	}
	else {
		result = "Wrong!";
	}

	var answerText = answerPlacement.innerHTML = result;
}

function fnSubmitAnswer() {
	if (!document.getElementById) return false;
	var btnSubmit = document.getElementById("btnSubmit");
	btnSubmit.onclick = fnAnswers;
}

addLoadEvent(fnSubmitAnswer);

I don’t see why it can’t just bloody work. It does work to some extent using the standards based method, but the result text disappears in the flash of a second. I really don’t want to use the innerHTML method, even though it works.

Can anyone else help with this? I can’t find any solutions to this problem anywhere, at least from the searches I’ve done.

Anyone…? My JavaScript heroes @paul_wilkins, @markbrown4, @cpradio, @AussieJohn, @felgall, and @Stomme_poes?

Hi Andrew,

Event handlers pass the event to the callback where you can cancel the default action.
The reason you see the flash is that it’s actually submitting the form.


function fnAnswers(event) {
    event.preventDefault();
    ...
}

There’s nothing wrong with using innerHTML too.

Mark, Mark, Mark…WHAT would I do without you?! Thank you!

But, in all seriousness, how the hell am I supposed to know about event.preventDefault()? That isn’t referenced or mentioned in any of the JavaScript books I have, nor any of the tutorials or articles I’ve read.

…Yet when you type event.preventDefault() in Google…It’s an entirely different matter! It’s mentioned in the Document Object Model (DOM) Level 3 Events Specification, [URL=“https://developer.mozilla.org/en/DOM/event.preventDefault”]Mozilla Developer Network, and [URL=“http://developer.yahoo.com/yui/examples/event/eventsimple.html”]YUI Library Examples: Event Utility: Simple Event Handling and Processing.

That’s annoying. I mean, if I would have known such a method existed for these purposes, well, I would have used it. But even then, I didn’t know what my actual problem was, and if you don’t know what the problem is, you can’t possibly seek out a solution to it. There’s loads of methods I could use…But which is the right one that’s going to solve my problem? I don’t know until I know what the problem is!

Anyway, thank you Mark. And also thank you again Mike for helping me with the earlier issues, if you didn’t help I wouldn’t be at the stage I’m at now! Plus fixing some silly and careless mistakes on my part.

Which JavaScript books have you got? I would have thought every book teaching anything about events should have it right near the top.
Simply Javascript is actually quite an excellent little book and includes how to work with events.
Another one is Eloquent Javascript http://eloquentjavascript.net/chapter13.html
Actually, flicking through that chapter I remember now there’s a compatibility issue with preventDefault, IE needs event.returnValue = false; instead.

A simpler method is just to return false in an event handler to preventDefault across the board. This doesn’t stop event bubbling though.
I’d really just recommend grabbing one of the addEvent functions to handle the cross browser issues, I just rely on libraries to take care of that these days.

…Yet when you type event.preventDefault() in Google…It’s an entirely different matter! It’s mentioned in the Document Object Model (DOM) Level 3 Events Specification, [URL=“https://developer.mozilla.org/en/DOM/event.preventDefault”]Mozilla Developer Network, and [URL=“http://developer.yahoo.com/yui/examples/event/eventsimple.html”]YUI Library Examples: Event Utility: Simple Event Handling and Processing.

That’s annoying. I mean, if I would have known such a method existed for these purposes, well, I would have used it. But even then, I didn’t know what my actual problem was, and if you don’t know what the problem is, you can’t possibly seek out a solution to it. There’s loads of methods I could use…But which is the right one that’s going to solve my problem? I don’t know until I know what the problem is!

Anyway, thank you Mark. And also thank you again Mike for helping me with the earlier issues, if you didn’t help I wouldn’t be at the stage I’m at now! Plus fixing some silly and careless mistakes on my part.

The moz docs are great and http://www.quirksmode.org/ has all the info on browser compatibility.

I have Simply JavaScript, DOM Scripting, JavaScript: The Definitive Guide 6th Edition, JavaScript: The Good Parts, and Head First JavaScript. So, quite a few.

Which versions of IE? I just checked the script in IE9 and it’s fine.

To end this thread I wanted to show everyone what I was trying to achieve from the first / original post and have now, finally, achieved, with the help of Mike and Mark :slight_smile: Plus, if anyone else wants to do something similar to what I wanted to do, they can just copy, paste, and edit the code here! :slight_smile: So, here is the complete working code!

HTML File

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Quick Question</title>
		<link rel="stylesheet" media="screen" href="quick-question.css">
	</head>

	<body>
		<div id="quick-question">
			<p class="header">Quick Question</p>

			<p class="question">Who invented the World Wide Web?</p>

			<form name="questionOne" method="post" action="#">
				<label for="answer">Answer:</label>
				<input type="text" value="" id="answerOne" name="answerOne" class="required" />
				<input type="submit" value="Submit" id="btnSubmit" name="btnSubmit" />
				<div id="answerPlacement"></div>
			</form>
		</div>

		<script src="quick-question.js"></script>
	</body>
</html>

JavaScript File

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function fnAnswers(event) {
	if (!document.getElementById) return false;

	// Answer Response

	function fnAnswerResponseCorrect() {
		var answerParagraph = document.createElement("p");
		var answerPlacement = document.getElementById("answerPlacement");
		answerPlacement.appendChild(answerParagraph);
		var answerText = document.createTextNode("Correct");
		answerParagraph.appendChild(answerText);
		answerParagraph.style.color = "Green";
	}

	function fnAnswerResponseWrong() {
		var answerParagraph = document.createElement("p");
		var answerPlacement = document.getElementById("answerPlacement");
		answerPlacement.appendChild(answerParagraph);
		var answerText = document.createTextNode("Wrong");
		answerParagraph.appendChild(answerText);
		answerParagraph.style.color = "Red";
	}

	function fnRemoveElement() {
		var answerPlacement = document.getElementById("answerPlacement");

		while (answerPlacement.firstChild) {
			answerPlacement.removeChild(answerPlacement.firstChild);
		}
	}

	// Question & Answer One

	if (document.questionOne.answerOne.value == "Tim Berners-Lee") {
		event.preventDefault();
		fnRemoveElement();
		fnAnswerResponseCorrect();
	}
	else {
		event.preventDefault();
		fnRemoveElement();
		fnAnswerResponseWrong();
	}
}

function fnSubmitAnswer() {
	if (!document.getElementById) return false;
	var btnSubmit = document.getElementById("btnSubmit");
	btnSubmit.onclick = fnAnswers;
}

addLoadEvent(fnSubmitAnswer);

Now that it’s actually working correctly, as I intended it to and all of the obvious errors have been fixed, does anyone fancy doing a code review? :slight_smile:

Honestly, I’d just do something like this. Keeping your code as small as possible is almost always the right thing to do.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Quick Question</title>
<link rel="stylesheet" media="screen" href="quick-question.css">
</head>

<body>
<div id="quick-question">
	<p class="header">Quick Question</p>

	<p class="question">Who invented the World Wide Web?</p>

	<form id="questionOne" name="questionOne" method="post" action="#">
		<label for="answer">Answer:</label>
		<input type="text" value="" id="answerOne" name="answerOne" class="required">
		<input type="submit" value="Submit" id="btnSubmit" name="btnSubmit">
		<div id="answerPlacement"></div>
	</form>
</div>

<script>
var $ = function(id) { return document.getElementById(id) }
$('questionOne').onsubmit = function(event) {
  $('answerPlacement').innerHTML = $('answerOne').value == "Tim Berners-Lee" ? 'yep': 'nope';
  return false;
}
</script>
</body>
</html>

I know that keeping your code as small as possible is always the right thing to do, but at the sacrifice of what?

I have you, Mike ([URL=“http://www.sitepoint.com/forums/showthread.php?864627-Text-Validation-Not-Working&p=5148151&viewfull=1#post5148151”]here), and [URL=“http://www.sitepoint.com/forums/member.php?101685-centered-effect”]centered effect ([URL=“http://www.sitepoint.com/forums/showthread.php?870172-Code-Review-Quick-Question-Script&p=5160272&viewfull=1#post5160272”]here) telling me I could just use innerHTML to do what I want. And that’s great, because it works.

But then I have Jeremy Keith in his DOM Scripting book telling me, on page 125:

And then Michael Morrison in his Head First JavaScript book telling me, on page 352:

And then on page 365:

So, what do I do? Follow the advice of three people on the SitePoint Community Forums, or what two book authors suggest? Who’s right and who’s wrong?

All I want to do is to do it the right way, the web standards way. All of my pages are HTML and CSS validated, why not add JS validation to that too? If I’m using standard HTML and CSS, why not use standard JS too through the DOM?

I guess my question is, what advantage does using innerHTML have over inserting content through the DOM standard?

But, please don’t get me wrong, I appreciate all that you and others have said and will say in this thread and others. But, it’s like in some threads I read on here and over at stackoverflow, some people that are looking to do something in plain old JavaScript want to learn how-to do it and then someone comes along and says “You can do it so much easier and simpler if you use jQuery”…I don’t want to use jQuery though, and if I was using it, if it’s so much simpler and easier than using plain old JavaScript, why are there as many jQuery books available as there is JavaScript books? It makes me think that just maybe, using jQuery isn’t as easy and simple as people make it out to be :slight_smile:

I really respect Jeremy Keith, but I’m sure he uses innerHTML as well :wink: I don’t think you’re sacrificing anything by using it, and you’re gaining a few things.
If anyone says never to use innerHTML on principle, yes, they’re wrong about that.
Those arguments aren’t really relevant to your issue at hand here.

innerHTML and outerHTML are actually a part of the HTML5 spec. Part paving the cow paths, part it’s often the best tool for the job.
http://html5.org/specs/dom-parsing.html#innerhtml

All I want to do is to do it the right way, the web standards way. All of my pages are HTML and CSS validated, why not add JS validation to that too? If I’m using standard HTML and CSS, why not use standard JS too through the DOM?
I guess my question is, what advantage does using innerHTML have over inserting content through the DOM standard?

The big one is that it has always been much faster than building up a DOM dynamically.
http://www.quirksmode.org/dom/innerhtml.html

But, please don’t get me wrong, I appreciate all that you and others have said and will say in this thread and others. But, it’s like in some threads I read on here and over at stackoverflow, some people that are looking to do something in plain old JavaScript want to learn how-to do it and then someone comes along and says “You can do it so much easier and simpler if you use jQuery”…I don’t want to use jQuery though, and if I was using it, if it’s so much simpler and easier than using plain old JavaScript, why are there as many jQuery books available as there is JavaScript books? It makes me think that just maybe, using jQuery isn’t as easy and simple as people make it out to be :slight_smile:

The “Don’t reach for jQuery” argument could be written in support of innerHTML.

Essentially it says “Don’t include a big library to do a small thing”
I’m saying “Don’t include a big script to do a small thing” :slight_smile:

There’s no right and wrong, both solutions work. Your script with the DOM methods was a heck of lot more work for not any real benefits though.