0

I am building <datalist> options as below:

data.suggestions.forEach(function(element) {
    $('datalist').append(`<option value="${element['description'].split(' ')[0].replace(/<[^>]*>/g, '')}">${element['description']}</option>`);
});

My element['description'] has HTML tags, which I would like to preserve. However, the appended children have their HTML formatting stripped.

Please advise.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Moshe Shmukler
  • 1,270
  • 2
  • 21
  • 43

1 Answers1

0

Split function returns an array of element. If you want only the first word, then try this

var str = element['description'].substring(0, element['description'].indexOf(' '))).replace(/<[^>]*>/g, '');

then

$('datalist').append('<option value="+str+" >element['description']</option>');
tiborK
  • 385
  • 1
  • 6