0

I have a function that gets the original String and slice it. How do it display the results in HTML?

function parsingfunction()
{
    var originalString = entity.getGenreSolr();
    var results = originalString.slice(1, -1);
    return results; 
}
</script>
<div class = "Option_Title_summary_summary">
<p>Genre List: ${parsingFunction}</p>
1
  • It's a valid question and should not have been downvoted before. Commented Oct 18, 2020 at 14:05

1 Answer 1

1

Well the code you are using is incorrect because we can't update HTML with JavaScript variables this way, you have to update the HTML from JavaScript instead.

Your code will be like this:

function parsingfunction() {
  /*var originalString = entity.getGenreSolr();
  var results = originalString.slice(1, -1);
  return results;*/
  return ["AAA", "BBB", "CCC"].join(" -- ");
}

document.getElementById("genreList").textContent = "Genre List: " + parsingfunction();
<div class="Option_Title_summary_summary">
  <p id="genreList"> </p>

I commented your old code to avoid Exceptions with undefined variables and I gave the id="genreList" to the paragraph so I can get it in the JS code.

Note:

There's another solution but it's not very recommended in JavaScript, by using document.write():

<script>
  function parsingfunction() {
    /*var originalString = entity.getGenreSolr();
    var results = originalString.slice(1, -1);
    return results;*/
    return ["AAA", "BBB", "CCC"].join(" -- ");
  }
</script>
<div class="Option_Title_summary_summary">
  <script>
    document.write('<p> Genre List: ' + parsingfunction() + '</p>');
  </script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.