Skip to content Skip to sidebar Skip to footer

How Do I Display A Javascript Variable Inside My Html Tags In Jsp?

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();

Solution 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:

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

document.getElementById("genreList").textContent = "Genre List: " + parsingfunction();
<divclass="Option_Title_summary_summary"><pid="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>functionparsingfunction() {
    /*var originalString = entity.getGenreSolr();
    var results = originalString.slice(1, -1);
    return results;*/return ["AAA", "BBB", "CCC"].join(" -- ");
  }
</script><divclass="Option_Title_summary_summary"><script>document.write('<p> Genre List: ' + parsingfunction() + '</p>');
  </script>

Post a Comment for "How Do I Display A Javascript Variable Inside My Html Tags In Jsp?"