Quantcast
Channel: SharePoint 2013 - General Discussions and Questions forum
Viewing all articles
Browse latest Browse all 26374

[Forum FAQ] How to remove div characters from multiline textbox field in SharePoint 2013

$
0
0

Scenario:

Need to avoid the div tags and get data alone from multiline textbox field using JavaScript Client Object Model in SharePoint 2013.

Solution:

We can use a regular expression to achieve it.

The steps in detail as follows:

1. Insert a Script Editor Web Part into the page.

2. This is the complete code, add it into the Script Editor Web Part and save.

<script type="text/javascript">

    ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");

    function retrieveListItems() {

        // Create an instance of the current context to return context information
        var clientContext = new SP.ClientContext.get_current();

        //Returns the list with the specified title from the collection
        var oList = clientContext.get_web().get_lists().getByTitle('CustomListName');

        //use CAML to query the top 10 items
        var camlQuery = new SP.CamlQuery();

        //Sets value that specifies the XML schema that defines the list view
        camlQuery.set_viewXml('<View><RowLimit>10</RowLimit></View>');

        //Returns a collection of items from the list based on the specified query
        this.collListItem = oList.getItems(camlQuery);

        clientContext.load(this.collListItem, 'Include(Title,MultipleText)');

        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }

    function onQuerySucceeded() {

        //Returns an enumerator to iterate through the collection
        var listItemEnumerator = this.collListItem.getEnumerator();
       //Remove div tag use a regular expression
        var reg1 = new RegExp("<div class=\"ExternalClass[0-9A-F]+\">[^<]*", "");
        var reg2 = new RegExp("</div>$", "");

        //Advances the enumerator to the next element of the collection
        while (listItemEnumerator.moveNext()) {
            //Gets the current element in the collection
            var oListItem = listItemEnumerator.get_current();
            alert(oListItem.get_item('MultipleText').replace(reg1, "").replace(reg2, ""));
        }
    }

    function onQueryFailed(sender, args) {

        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }</script>

Result:<o:p></o:p>

References:

http://www.w3schools.com/jsref/jsref_obj_regexp.asp


Please click to vote if the post helps you. This can be beneficial to other community members reading the thread.


Viewing all articles
Browse latest Browse all 26374

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>