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
  • 65,304
  • 15
  • 70
  • 95
Moshe Shmukler
  • 1,204
  • 2
  • 20
  • 38

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
  • 357
  • 1
  • 6