Posts from April 2010.

MySQL Toggle a Fields Value

fetchit-21
UPDATE `table_name` 
SET `field_name` = (SELECT CASE `field_name` WHEN 1 THEN 0 ELSE 1 END) 
WHERE `id_column` = 1

Or with text strings

UPDATE `table_name` 
SET `field_name` = (SELECT CASE `field_name` WHEN 'foo' THEN 'bar' ELSE 'foo' END) 
WHERE `id_column` = 1

Validation of html within inline javascript

fetchit-21

If you are finding you need to use javascript inside an html or xhtml file and it won’t validate, then here is a little tip for you.

If you surround all your javascript with <![CDATA[ and ]]> tags, all will come good.

The CDATA is seen by the validator as data which doesn’t need checking. So this:

<script>
document.getElementById('container').innerHTML('<p>hello</p>');
</script>

becomes this:

<script>
<![CDATA[
document.getElementById('container').innerHTML('<p>hello</p>');
]]>
</script>

Another option is to escape < and > characters server-side / in the file and then unescape with javascript

<script>
document.getElementById('container').innerHTML('&lt;p&gt;hello&lt;/p&gt;'.replace('/&lt;/','<').replace('/&gt;/','>'));
</script>