How to assign Multiple line html code as a value to a javascript variable? - Stack Overflow

This my code..<!DOCTYPE html><html><head><script>function generate(){document.g

This my code..

 <!DOCTYPE html>
    <html>
     <head>
    <script>
    function generate(){
    document.getElementById("show").style.display = "block";	
    var name = document.getElementById("name").value;
    var school_name = document.getElementById("school_name ").value;
    var school_site= document.getElementById("school_site ").value;

    var content= "<h2>Student Details:</h2>"+"/n"+
    "<div align='justify'>
     <p>"+name+"is studing in "+school_name+"</p>"+"/n"+
    "<p>Visit site: <a href='http://"+school_site+"'>http://"+school_site+"</a></p></div>";
    
    document.getElementById("displayarea").innerHTML = content;
    }
    </script>
     </head>
    <body>
    
      Privacy Policy Page
      <p>Name:</br>  <input type="text" name="name" id="name"></p>
     <p>School Website:</br>  <input type="text" name="school_site" id="school_site"></p>
    <p>School Name:</br>  <input type="text" name="school_name" id="school_name"></p>
    
    <button id="click" onclick="generate()">Generate</button>
    
     <div style="display:none" id="show">
    <div style="height:200px; width:540px; overflow:auto;" id="displayarea">
    
    </body>
</html>

This my code..

 <!DOCTYPE html>
    <html>
     <head>
    <script>
    function generate(){
    document.getElementById("show").style.display = "block";	
    var name = document.getElementById("name").value;
    var school_name = document.getElementById("school_name ").value;
    var school_site= document.getElementById("school_site ").value;

    var content= "<h2>Student Details:</h2>"+"/n"+
    "<div align='justify'>
     <p>"+name+"is studing in "+school_name+"</p>"+"/n"+
    "<p>Visit site: <a href='http://"+school_site+"'>http://"+school_site+"</a></p></div>";
    
    document.getElementById("displayarea").innerHTML = content;
    }
    </script>
     </head>
    <body>
    
      Privacy Policy Page
      <p>Name:</br>  <input type="text" name="name" id="name"></p>
     <p>School Website:</br>  <input type="text" name="school_site" id="school_site"></p>
    <p>School Name:</br>  <input type="text" name="school_name" id="school_name"></p>
    
    <button id="click" onclick="generate()">Generate</button>
    
     <div style="display:none" id="show">
    <div style="height:200px; width:540px; overflow:auto;" id="displayarea">
    
    </body>
</html>

"content" is the javascript variable.

  1. I need to assign HTML code as value for "content" variable,
  2. And i also need to add some Javascript variable inside the HTML code,
  3. How to add javascript variable in html Hypertext link?
Share Improve this question edited Dec 8, 2016 at 5:25 LF-DevJourney 28.6k30 gold badges165 silver badges316 bronze badges asked Dec 8, 2016 at 5:22 Manoj Kumar KManoj Kumar K 731 gold badge2 silver badges10 bronze badges 1
  • no need to add new line character '/n' – Ajay Narain Mathur Commented Dec 8, 2016 at 5:25
Add a ment  | 

5 Answers 5

Reset to default 2

There are many ways to achieve this. For a simple use-case, you can use an array of string to perform work and at the end you can join with "
" or "\n".

var template = [
        "<h2>Student Details:</h2>",
        "<div align='justify'><p>"+name+"is studing in "+school_name+"</p>",
        "<p>Visit site: <a href='http://"+school_site+"'>http://"+school_site+"</a></p></div>"
].join("<br/>");

For more plex case, I will say use jquery or Plain JavaScript method. As given below.

function generate(){
    document.getElementById("show").style.display = "block";
    var name = document.getElementById("name").value;
    var school_name = document.getElementById("school_name").value;
    var school_site= document.getElementById("school_site").value;
    //jQuery:
    var node = $('<div></div>')
        .hide()
        .append($('<table></table>')
            .attr({ cellSpacing : 0 })
            .addClass("text")
        );
    //Plain JavaScript
    var h2 = document.createElement("h2");
    h2.textContent = "Student Details:";
    var div = document.createElement("div");
    var p1 = document.createElement("p");
    p1.textContent = name+"is studing in "+school_name;
    var p2 = document.createElement("p");
    p2.textContent = "Visit site: ";
    div.appendChild(p1);
    div.appendChild(p2);
    //add attribute node
    var node = document.getElementById("div1");
    var a = document.createAttribute("my_attrib");
    a.value = "newVal";
    node.setAttributeNode(a);
    //Once done return as string
    return div.outerHTML;
}

You have extra space in id in school_name and school_site`.

So it is not being recognized and you are getting exception. Also your syntax to concatenate string is also incorrect.

var school_name = document.getElementById("school_name").value;
var school_site= document.getElementById("school_site").value;

Your full javascript code would be like this

<script>
    function generate(){
        document.getElementById("show").style.display = "block";    
        var name = document.getElementById("name").value;
        var school_name = document.getElementById("school_name").value;
        var school_site= document.getElementById("school_site").value;

        var content= "<h2>Student Details:</h2>"+"/n"+
        "<div align='justify'>"+
         "<p>"+name+"is studing in "+school_name+"</p>"+"/n"+
        "<p>Visit site: <a href='http://"+school_site+"'>http://"+school_site+"</a></p>";

        document.getElementById("displayarea").innerHTML = content;
    }
</script>

You can write a string on multiple lines using template literals, i.e. using the character " ` ".

You can easily integrate variables using ${yourVar} in the template literal

Example:

let lit = "literal";
var content = `This string
    uses a template ${lit}`;
console.log(content);

Note: this is an ES6 feature, aka the not so new JavaScript that is not yet fully supported by browsers. To make this code patible with older browsers, use a transpiler like babel

You have to use <br> instead of '/n' while assigning to javascript variable.

The problem as I see it is you have hit enter in the mid of string and you have extra space in the id selector.

Don't hit enter or use tilt ` to declare string instead of quotes.

<!DOCTYPE html>
<html>

<head>
  <script>
    function generate() {
      document.getElementById("show").style.display = "block";
      var name = document.getElementById("name").value;
      var school_name = document.getElementById("school_name").value;
      var school_site = document.getElementById("school_site").value;

      var content = "<h2>Student Details:</h2>" +
        "<div align='justify'><p>" + name + "is studing in " + school_name + "</p>" +
        "<p>Visit site: <a href='http://" + school_site + "'>http://" + school_site + "</a></p></div>";

      document.getElementById("displayarea").innerHTML = content;
    }
  </script>
</head>

<body>

  Privacy Policy Page
  <p>Name:</br>
    <input type="text" name="name" id="name">
  </p>
  <p>School Website:</br>
    <input type="text" name="school_site" id="school_site">
  </p>
  <p>School Name:</br>
    <input type="text" name="school_name" id="school_name">
  </p>

  <button id="click" onclick="generate()">Generate</button>

  <div style="display:none" id="show">
    <div style="height:200px; width:540px; overflow:auto;" id="displayarea">

</body>

</html>

Suggestion : No need to use /n for new line, h2 is block element no need of break too.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745412054a4626579.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信