WSS v3 – Adding Line Break to a Survey

I came across this little issue this week and found a solution that tested successfull. It is quite easy to implament. The only possible downside is that it isn’t going to effect the entire site collection if the heiarchy is broken….

Thanks! Mark Deraeve

by mderaeve 23. May 2009 02:37

A user is trying to make a survey. Now, she needed to create a header for each new page in the survey. She tried to use the textbox and hit enter in the question field, that’s what I would do to. And it appears as if the enter are accepted. But when you look at the question, you will notice that the line break disappeared. As I was looking for a solution, I found that in the previous version, this was possible but not in WSS 3.0! So after looking a little more, this site gave a good solution, I decided to use it, but with a slightly different approach.

The thing is that we will implement a javescript that will look for elements with classname : ‘ms-formlabel’. Then it will replace the word linebreak with a <BR>. This solution you can find on the site I mentioned above. But they suggest to place a content editor webpart on each page that uses the survey, like the newform.aspx of each survey. Since I’m using a WSS site specially for surveys, I don’t want to do this every time I create new survey. So I added the code to the default masterpage of the site. In the head section of the page I placed a function called: setLineBreaks()

<HEAD runat="server">
            …
            <script>
            function setLinebreaks()
            {
                        var className;
                        className = 'ms-formlabel';

                        var elements = new Array();
                        var elements = document.getElementsByTagName('td');

                        for (var e = 0; e < elements.length; e++)
                        {
                        if (elements[e].className == className){
                        elements[e].innerHTML = elements[e].innerHTML.replace(/linebreak/g,'<br/>');}
                        }
            }
            </script>
</HEAD>

Then I call this function on the pageload event like this (the code I added is in bold):

<BODY scroll="yes"
onload="javascript:if (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();setLinebreaks();">

So now every time you enter the word linebreak in a question a new line will be started. (It will be replaced with a <br>)

Some screenshots of the results:

linebreaklinebreak2


2 Responses to WSS v3 – Adding Line Break to a Survey

  1. Great solution!

    However, is there any way this can be applied to a survey answer. specifically when the answer type is set to ‘Rating Scale’?

  2. Yes, the response references the same data and formatting that the survey uses.