Dailycode.info

Short solution for short problems

How to add a line break or new line in a SharePoint survey question

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 language="javascript" type="text/javascript">
            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>)