views:

39

answers:

2

I'm working on get my site validated in an attempt to iron out issues with it showing up incorrectly on IE, and I'm getting an error: document type does not allow element "script" here. It's from me placing the <script> tag within the <select> tag on the javascript drop down menu form I have.

Is there any way to get this validated? Or any work arounds?

The site in question is Blue Devil Books

And the specific code snippit is:

<noscript>This site requires that Javascript is enabled on your browser!</noscript>
<select name="CLASS"><script type="text/javascript">dynlist.printOptions("CLASS")</script>
</select>&nbsp;
<select name="SEC"><script type="text/javascript">dynlist.printOptions("SEC")</script>   
</select>   

<input type="submit" value="Search" />
</form></div></div>
A: 

Select elements can contain only <option> elements. There is no way to make it valid without writing according to the spec.

You could write out the entire select element with JS but, frankly, I'd move the logic server side and remove the dependency on JS entirely.

David Dorward
Is there a way to make it work at least in all browsers? It seems as though some versions of IE won't show the drop down lists. I'd make it server side, but I don't know how to make dynamic lists in anything besides JS.
Parker
It isn't worth trying to get broken code to work. Make the code not broken. A bunch of option elements is a very simple concept, it is trivial to do in any server side language. Pick one and learn the basics.
David Dorward
+1  A: 

Put your script outside the selects completely and then use innerHTML to modify their content. The following example assumes two things:

1) The name of the select element is always the same as the variable passed to printOptions e.g. if the name is "CLASS" then the contents should be printOptions("CLASS")

2) printOptions should be replaced with options which is an identical method except that instead of printing the options, it should instead return them.

<noscript>This site requires that Javascript is enabled on your browser!</noscript>
<select name="CLASS"></select>&nbsp;
<select name="SEC"></select>   

<input type="submit" value="Search" />
</form></div></div>

<script type="text/javascript">
    var selects = document.getElementsByTagName("select")
    for (i = 0; i < selects.length; i++) {
        var name = selects[i].getAttribute("name");
        var options = dynlist.options(name);
        selects[i].innerHTML = options;
    }
</script>
Rupert
Thanks! This is what I was looking for, much appreciated!
Parker