4

I have a Javascript variable that contain HTML code as follows:

 <DIV>Hello World<BR>Welcome to SharePoint Stack Exchange</DIV>

So, how do I insert it into the SharePoint 2007 Multi line text field (The meaning of a SharePoint 2007 Multi Line text field is shown in the picture below)?

enter image description here

For your info, I have tried using SPServices to insert that piece of HTML code but it just cannot insert into the SharePoint text field. However, if I use escape(), I am able to insert into the text field but the text is not what I wanted.

Any Insight?



[Update - Additional Information]

I typed the following into the textarea called "newWordDesc" in CODE A

 <DIV>Hello World<BR>Welcome to SharePoint Stack Exchange</DIV>

and with the help of CODE B, it is supposed to store it into a field called "Description" inside SharePoint 2007.

CODE A - HTML TextArea using Javascript

document.write('<textarea id="newWordDesc" wrap="soft"></textarea>');   

CODE B - SPservices

$(document).ready(function() {

        CreateNewItem($("#newWordDesc").val());

        function CreateNewItem(newWordDesc) {
            $().SPServices({
                operation: "UpdateListItems",
                async: false,
                batchCmd: "New",
                listName: "MyList",
                valuepairs: [["Description", newWordDesc]],
                completefunc: function(xData, Status) {
                   $("#responseStatus").html(Status);
                   $("#responseXML").text(xData.responseXML.xml);

                }
            });
        }

Would appreciate if any insight provided.

2
  • From your title.. it sounds like you're trying to insert JavaScript into the field? Or is it just plain HTML? Anyways... I'm guessing your SPServices call isn't working correctly.. have you see this page? spservices.codeplex.com/…
    – Kit Menke
    Commented Nov 3, 2011 at 13:50
  • Hi Kit, I tested with the plain non-HTML, just normal textarea, it work quite fine but I tried some solution using javascript rich text editor (such as CLEditor), it seems that the whole textarea (that contain those HTML formatting) cannot be insert into SPServices. I have already gone thru the link you provided and appreciate your comments. Commented Nov 8, 2011 at 9:07

2 Answers 2

2

You can easily use jQuery to push the value of your variable into the field. Say your field had a title of "MyTextField", you'd do something like $("textarea[title='MyTextField']").append(myVar); if you wanted to append the text, or $("textarea[title='MyTextField']").html(myVar); if you wanted to replace whatever was in the field with the value of your variable. Obviously you may have to tweak the selector to get it right in your environment (I'm trying to think back to what the DOM around multiline fields looks like in 2007). You can use the DOM explorer in the Chrome debug tools, or IE Dev tools to help you build the correct selector, but once you have that it should be a simple jQuery append() or html() call.

I believe multiline fields are rendered as textareas, not input fields (and I just powered down my VM so I can't validate that). If it is a textarea, remember that text areas use the format of <textarea name="x">My Value</textarea> as opposed to an input which uses <input name="x" value="My Value"/>. As such, you should use append() or html() as opposed to val(), since there is no value attribute.

2
  • Hi webdes03, I am looking on how to insert html formatting from a javascript rich text plugin on a html textarea and insert into the sharepoint 2007. Commented Nov 8, 2011 at 9:08
  • It looks like no one understood the OP's question. He wants to use SPServices to update the value of a multi-line text field, and have line breaks saved in the text that is written by SPServices to the SharePoint list. I have the same requirement, and have not yet found a way to do it. Commented Apr 28, 2017 at 16:53
2

To get the content of a textarea, you need to use text(), not val():

$("#newWordDesc").text()

Not sure if it helps, but I have a working example of textarea with tags on this page, feel free to look at the source code:

http://usermanagedsolutions.com/SharePoint-User-Toolkit/Pages/Easy-Tabs-v5.aspx

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.