I am getting one validation error due to my use of data-href=\"\" in the context of making a whole DIV clickable. The JS and HTML is below. What could I do to make this W3C
Since data-*
is part of the HTML5
specification, you need to use the html5-doctype:
<!DOCTYPE html>
Any other doctype will parse your site according to HTML4 or XMHTML-Rules, where the data-*
Attribute is invalid.
edit:
You should always start your html with the proper doctype. Since <!DOCTYPE html>
triggers standards-mode in all browser, this is "the only right way" except you really want XHTML for example (although I can't see a reason why you should).
edit2:
You might want to consider using an a
anchor-tag instead, since it already has an href and is considered to be clickable?
If possible, you should consider using a doctype that supports data-*
attributes. For HTML5, that's:
<!DOCTYPE html>
You cool with adding the attribute through jQuery? Or do we need a more dynamic solution?
$(".linked").attr('data-href', 'link.html').click(function(){
window.location = $(this).attr("data-href");
return false;
});
also:
Considering that this isn't a WC3 compliant method have you thought about moving this into the Title attribute? Remove the '.html'line, and in your click function:
HTML:
<div class="" title="link"></div>
jQuery
$(".linked").click(function(){
var dataHref = $(this).attr('title');
window.location = dataHref.html;
return false;
});
Then again, we run into issues with propagation. I don't know how your links are being propagated, whether you hard-code them or you dynamically create them with server side script, so the latter and previous solutions still not might be good enough for you.
I was downvoted, as the OP said that the solution provided did not work -- The solution provided works completely fine. Please see the fiddle below, and make sure you don't have errors in your javascript before you downvote someone.
http://jsfiddle.net/n68A8/
data- attributes are part of the HTML5 specification so if your document does not have the HTML5 Doctype, the validator will produce an error.
In the meantime, there is no problem of using those attribute with an HTML4 doctype, the browser will do nothing with it and you'll be able to work with them - the page will simply not validate to the W3C validator.