views:

376

answers:

3

I am making a site with DOCTYPE XHTML Strict and it complains if I have an input button or button element outside a form.

So I put it in a form and it complains that I don't have an action attribute.

So I put in an action attribute but then the buttons postback to the page, which I don't want.

How can I just have a normal jQuery button with bound event in XHTML strict?

<form action="">
    <div>
     <input id="button1" type="button" value="highlight it"  /> 
     <input id="button2" type="button" value="toggle title" />
     <p>here is another paragraph</p> 
    <div id="show1">What color is the sky?</div>
    <div id="answer1">blue</div>
     <button id="button1">Show Answer</button>
    </div>
</form>
A: 

This is why you shouldn't use xhtml strict. If the only reason you do something is to please the validator, then the validator is wrong. Use html5 instead, and do it the way you did originally, ie no form.

Marius
The general idea of switching to XHTML strict is so that I can easily parse it, e.g. PHP's SimpleXML or C#'s LINQ. I just see that as a big bonus. What is the best DOCTYPE that balances flexibility and XML parsing?
Edward Tanguay
You can use XHTML5 if you like. But HTML5 is nothing to do with this question, input-without-form is just as valid in XHTML1.0 as HTML5 as HTML4.
bobince
+1  A: 

The trick is to return false from your click() handler, like so:

$("#button_id").click(function() { /* do stuff.. */ return false; })
Max A.
+1  A: 

I am making a site with DOCTYPE XHTML Strict and it complains if I have an input button or button element outside a form.

Nope, it's perfectly acceptable to have an input/button with no form. Indeed, there is no way in DTD to express the limitation that an input must have a form ancestor, so XHTML couldn't ban it if it wanted to. If you had a validation error, it wasn't that. It was probably this:

<button id="button1">Show Answer</button>

You've already got an element with id="button1". ids must be unique.

When you aren't going to submit a form, you generally shouldn't include the form element. However there is one case you need to: to group a set of radio inputs that have the same control name. If they are left with no form ancestor they don't know they're connected, so browsers tend not to make them exclusive.

In this case, the usual idiom is to set the required action attribute to "#", and add an onsubmit handler to the form that always returns false so that when JS is enabled the form will never be submitted. Don't put this return false on the buttons themselves, as the form may be submitted by other means than a button click: in particular, an Enter keypress.

So I put in an action attribute but then the buttons postback to the page, which I don't want.

An <input type="button"> won't submit the form. <button> shouldn't either, but it does in IE due to a bug where the browser uses the wrong default type value. So you have to write <button type="button">.

bobince
thanks, the problem was it wasn't in a DIV tag, I don't need the FORM tag, works now
Edward Tanguay