views:

431

answers:

6

I'm using this (jQuery) to replace all <br>s with <br /> to clear out validation errors:

$("<br>").replaceAll("<br />");

but it doesn't reduce any validation errors. Does validator check the original source?

+5  A: 

Validators do not run javascript. They parse the HTML and compare it to the schema for the declared doctype.

You will need to replace the <br> in your source files / views and not on the client side.

Neil Aitken
+11  A: 

JQuery will only fire after the document has been rendered. The process of the page loading will happen like the following

  1. Request For page is made
  2. Page is sent to client machine
  3. Page Loads Up
  4. Page is validated
  5. JQuery Fires
  6. break tags are replaced

I would recommend just doing a site-wide find and replace on all <br> tags and replace them with <br />

GaryDevenay
I agree, anything you can do outside of Javascript, you should.
Pino
A: 

first, as has been said the validators check the html file, and does not run anything. beside, if you want to the html to be valid, there much more than
of course.. why don't you just use an html editor?

Itay
A: 

JQuery runs on the client. You must change your source code with basic replace command on notepad++ for instance.

drorhan
+2  A: 

There is no reason to do this. XHTML is dead. Switch your doctype to html 5 and go back to happily using unclosed tags:

<!DOCTYPE html>
Gabe Moothart
XHTML is not dead. XHTML 2 is no longer in active development, but there are still valid reasons to use XHTML, and it's not going anywhere. The problem with XHTML 1.1 is that most people don't serve their pages as xml, which is the main reason to use XHTML. But there are applications for this, so just because you choose not to use it, doesn't make it dead, and it doesn't make this a helpful answer.
idrumgood
Agree with the above comment, but in this case, it was helpful. I can change the doctype. :)
Nimbuz
Well then your question should have been "How can I validate a page that has `<br>` in it?"
idrumgood
+1  A: 

The HTML is parsed into the DOM model (step 3 in Gary's post), where <br> and <br /> are considered equal. Adding an element to an HTML page through JavaScript, whether you use jQuery or any other means, will parse your element and add it to the DOM. How the internal HTML looked like is not important anymore for what the browser's concerned.

You can see this for yourself if you use innerHTML. Place the following in any HTML document (doesn't matter if it's XHTML, HTML4 or HTML 3.2):

<p onclick="alert(this.innerHTML);">BR: <br />self close</p>
<p onclick="alert(this.innerHTML);">BR: <br>open</p>
<p onclick="alert(this.innerHTML);">BR: <BR>open capitals</p>

Load it in a browser and click on it.

On IE all three variants show up as "<BR>", on FF, Chrome, Opera all three variants show up as "<br>". This is how the browsers represent the HTML internally. Using valid HTML or invalid HTML with JavaScript will not change this. Worse: the internal HTML representation is not valid XHTML, even when the document is!

Abel