7

Our application can share code. For example user is sharing the html code as follows

<div id="nav-vert-one">

<ul>
{{for GroupCollection}}
<li>
 <a href="#" title="{{:Name}}" onclick="test()">      {{:Name}}</a>
('{{:GroupId}}')">

</li>
{{/for}}
</ul>

which is not in a perfect format .... Now i need to achieve the (auto) code formatting ...

i.e. after auto code format click, it should look like

<div id="nav-vert-one">
    <ul>
        {{for GroupCollection}}
        <li><a href="#" title="{{:Name}}" onclick="test()"> {{:Name}}</a>
('{{:GroupId}}')"></li>
        {{/for}}
    </ul>

So, is there ready made plugin available or is there any way(s) to achieve it either via jQuery or C#?

10
  • Is this a string you have on the client slide after the page is rendered? Or is this before rendering?
    – jyore
    Mar 7, 2013 at 12:20
  • It's invalid HTML. <a is missing the >.
    – mak
    Mar 7, 2013 at 12:41
  • 7
    Try github.com/einars/js-beautify or code.google.com/p/google-code-prettify Both of these also support HTML.
    – mak
    Mar 7, 2013 at 12:42
  • @jYore: (if with string you meant the whole code then yes, This is the string on the client side. Mar 7, 2013 at 13:49
  • 3
    I would need a bit more context.The Application is a Webage / Winforms / WPF /... ? How is the code shared, with Textarea , textfile, textbox, ... ? The Codeformating should happen in VS / special Control / ckeditor / ...? Thx in advance 4 the infos Mar 8, 2013 at 7:00

1 Answer 1

1

There is a jQuery plugin called jquery-htmlClean: http://code.google.com/p/jquery-clean/

I tried it with your code sample, this is the output:

<div>
    <ul>
        {{for GroupCollection}}
        <li>
            <a href="#" title="{{:Name}}">{{:Name}}</a> ('{{:GroupId}}')">
        </li> {{/for}}
    </ul>
</div>

You can try it here: http://www.antix.co.uk/Content/Demos/jQuery-htmlClean/Test.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.