0

I am getting the value from a text field and then inserting the value into a SP list. The problem is that when I check the console the field shows HTML.

var txtField = document.querySelector('#txtField');

I am inserting the following:

some test string

The when console logging the list of objects inserted is see the following in the console:

<div class="...some hexadecimal stuff...">some test string</div>

How do I remove the HTML tags so that I only get "some test string"?

3
  • Is this multiple lines of rich text field? Are you getting the HTML in REST API call response or from list form while using above querySelector (before saving item to list)? May 5, 2021 at 15:16
  • I am doing querySelector on input inserted into html form before inserting into SP list via REST API.
    – martinbshp
    May 5, 2021 at 15:30
  • Try using var txtFieldValue = txtField.value;. let me know if this works for you. May 5, 2021 at 15:35

1 Answer 1

0

Try using below solutions:

var txtField = '<div class="abc">some test string</div>';
var txtFieldValue = txtField.replace(/<[^>]*>?/gm, '');

OR

function stripHtml(html) {
   let tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent || tmp.innerText || "";
}
var txtField = '<div class="abc">some test string</div>';
var txtFieldValue = stripHtml(txtField);

OR (if you are using jQuery):

var txtField = '<div class="abc">some test string</div>';
var txtFieldValue = $(txtField).text();

Source: Strip HTML from Text JavaScript

Your Answer

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

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