Skip to content Skip to sidebar Skip to footer

Escaping Double Quotes In Html Event Handlers

How do I escape double quotes in an event handler in HTML? For example, how do I properly escape the bar, which is a string literal, in the following code?

And, you can mix them indeed in XHTML, try this in the W3 validator:

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="Content-Type"content="text/html; charset=utf-8" /><title>foo</title></head><body><divonclick='bar("foo");'></div></body></html>

There are some tutorials which said single quotes are not valid, but they are incorrect.

Solution 2:

XHTML prefers double quotes around the attributes. But you can still use single quotes inside the value. The follow for example is XHTML 1.0 Strict

<buttononclick="foo('bar')">Click Me</button>

I would suggest looking into progressive enhancement and moving away from the behavioral attributes.

Post a Comment for "Escaping Double Quotes In Html Event Handlers"