how to write directly on html file some data from a variable

Viewed 124

So i dont know how to ask this but i wanted to make a way to put info from a variable directly into an html file

so i have a js file like this

    const list = {
        "list1": [
            {
                "records": [
                    {
                        "user": "Someone",
                        "link": "link"
                    },
                    {
                        "user": "Someone 2",
                        "link": "link"                 
                    },
                ],
                
                /////////////////////////////

                "name": "SOMETHING",
                "author": "someone",
                "id": 12345678,
                "vid": "link",
                "iframe": "link"

                /////////////////////////////
            },

and that info from records so like list.list1[0].user and to make that string direcly appear on an html file and when clicking it it opens the "link" without using a function for every of those string

html looks like

<!DOCTYPE html>

<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="keywords" content="HTML, CSS, JavaScript">
<meta name="author" content="Alf">
<title>Example</title>
<link rel="stylesheet" media="screen and (min-device-width: 320px)"href="../../css/320style.css">
<link rel="stylesheet" media="screen and (min-device-width: 760px)"href="../../css/760style.css">
<link rel="stylesheet" media="screen and (min-device-width: 900px)"href="../../css/900style.css">
<link rel="stylesheet" media="screen and (min-device-width: 1200px)"href="../../css/1200style.css">
<link rel="icon" href="../../images/icon.png">
</head>

<body>

    <div class="navbar">
        <li>
        <button onclick="location.href='../../../index.html';">HOME</button>
        <button onclick="location.href='../../index.html';">LIST</button>
        <button class="legacybtn" onclick="location.href='../../legacy.html';">LEGACY</button>
        <button class="apibtn" style="float: right;" onclick="location.href='../../doc.html'">DOC</button>
        <button class="submitbtn" style="float: right;" onclick="submitRecord()">SUBMIT</button>
        <button class="submitbtn" style="float: right;" onclick="location.href='../../user.html';">USER</button>
        </li>
    </div>
    <center><img class="logo" src="../../images/logo.png"></center>

<div class="bg5">

    <p class="title" id="levelname"></p><a href="../2"><p class="ttitle"> ></p></a><br>
    <p class="subtitle" id="levelauthor"></p>

    <iframe class="vid" id="iframe1" frameborder="0" allowfullscreen></iframe><br><br><br>>

    <center><h1 class="subtitle" id="levelid"></h1></center>

    <div class="records">
        <h1 class="title2">RECORDS</h1>
        <p class="recordd2" id="record1x2" onclick="video1x2()"></p>
        <p class="recordd" id="record1x3" onclick="video1x3()"></p>
        <p class="recordd2" id="record1x4" onclick="video1x4()"></p>
        <p class="recordd" id="record1x5" onclick="video1x5()"></p>
    </div>
</div>

<center><p class="userpoint">&copy; Alf 2021</p></center>

<script src="../../js/main.js"></script>
<script src="../../js/level.js"></script>
<script src="../../js/video.js"></script>
<script src="../../js/jquery.js"></script>
<script src="../../js/jquery1.js"></script>
<script src="../../js/list.js"></script>
<script src="../../js/user.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>
    document.getElementById('levelname').innerText = shitty.list[0].name;
    document.getElementById('levelauthor').innerText = shitty.list[0].author;
    document.getElementById('levelid').innerText = "ID: " + shitty.list[0].id;

    document.getElementById('iframe1').src = shitty.list[0].iframe;

    document.getElementById('record1x2').innerText = shitty.list[0].records[0].user + " | " + shitty.list[0].records[0].hz;
    document.getElementById('record1x3').innerText = shitty.list[0].records[1].user + " | " + shitty.list[0].records[1].hz;
    document.getElementById('record1x4').innerText = shitty.list[0].records[2].user + " | " + shitty.list[0].records[2].hz;
    document.getElementById('record1x5').innerText = shitty.list[0].records[3].user + " | " + shitty.list[0].records[3].hz;
    
</script>

</body>

</html>

so i dont want to insert document.getElementById('record1x2').innerText = shitty.list[0].records[0].user + " | " + shitty.list[0].records[0].hz; and <p class="recordd2" id="record1x2" onclick="video1x2()"></p> for every string and i want something that directly write it and i something that automatically opens the link "link": "link" on a new tab without using a function like

function video1x2() {
    window.open(shitty.list[0].records[0].link);
}

i dont really know how to explain this thing but i just want not to copy and paste the same code to write all of those "records" but to use a single code that automatically do it, same for the link to open.

I hope you can understand a little bit at least what i want.

The final result should be like i add a line to "records" so i just add

{
                        "user": "Someone 3",
                        "link": "link"                 
                    },

and it write on the html.

Thanks :)

const list = {
        "list1": [
            {
                "records": [
                    {
                        "user": "Someone",
                        "link": "link"
                    },
                    {
                        "user": "Someone 2",
                        "link": "link"                 
                    },
                ],

                /////////////////////////////

                "name": "SOMETHING",
                "author": "someone",
                "id": 12345678,
                "vid": "link",
                "iframe": "link"

                /////////////////////////////
            },
<!DOCTYPE html>

<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="keywords" content="HTML, CSS, JavaScript">
<meta name="author" content="Alf">
<title>Example</title>
<link rel="stylesheet" media="screen and (min-device-width: 320px)"href="../../css/320style.css">
<link rel="stylesheet" media="screen and (min-device-width: 760px)"href="../../css/760style.css">
<link rel="stylesheet" media="screen and (min-device-width: 900px)"href="../../css/900style.css">
<link rel="stylesheet" media="screen and (min-device-width: 1200px)"href="../../css/1200style.css">
<link rel="icon" href="../../images/icon.png">
</head>

<body>

    <div class="navbar">
        <li>
        <button onclick="location.href='../../../index.html';">HOME</button>
        <button onclick="location.href='../../index.html';">LIST</button>
        <button class="legacybtn" onclick="location.href='../../legacy.html';">LEGACY</button>
        <button class="apibtn" style="float: right;" onclick="location.href='../../doc.html'">DOC</button>
        <button class="submitbtn" style="float: right;" onclick="submitRecord()">SUBMIT</button>
        <button class="submitbtn" style="float: right;" onclick="location.href='../../user.html';">USER</button>
        </li>
    </div>
    <center><img class="logo" src="../../images/logo.png"></center>

<div class="bg5">

    <p class="title" id="levelname"></p><a href="../2"><p class="ttitle"> ></p></a><br>
    <p class="subtitle" id="levelauthor"></p>

    <iframe class="vid" id="iframe1" frameborder="0" allowfullscreen></iframe><br><br><br>>

    <center><h1 class="subtitle" id="levelid"></h1></center>

    <div class="records">
        <h1 class="title2">RECORDS</h1>
        <p class="recordd2" id="record1x2" onclick="video1x2()"></p>
        <p class="recordd" id="record1x3" onclick="video1x3()"></p>
        <p class="recordd2" id="record1x4" onclick="video1x4()"></p>
        <p class="recordd" id="record1x5" onclick="video1x5()"></p>
    </div>
</div>

<center><p class="userpoint">&copy; Alf 2021</p></center>

<script src="../../js/main.js"></script>
<script src="../../js/level.js"></script>
<script src="../../js/video.js"></script>
<script src="../../js/jquery.js"></script>
<script src="../../js/jquery1.js"></script>
<script src="../../js/list.js"></script>
<script src="../../js/user.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>
    document.getElementById('levelname').innerText = list.list1[0].name;
    document.getElementById('levelauthor').innerText = list.list1[0].author;
    document.getElementById('levelid').innerText = "ID: " + list.list1[0].id;

    document.getElementById('iframe1').src = list.list1[0].iframe;

    document.getElementById('record1x2').innerText = list.list1[0].records[0].user
    document.getElementById('record1x3').innerText = list.list1[0].records[1].user
    document.getElementById('record1x4').innerText = list.list1[0].records[2].user
    document.getElementById('record1x5').innerText = list.list1[0].records[3].user
    
</script>

</body>

</html>

1 Answers

You can loop through records json array using $.each loop and then generate p tags inside this loop with user and link then append this generated html inside your records div

Demo Code :

const list = {
  "list1": [{
    "records": [{
        "user": "Someone",
        "link": "link1"
      },
      {
        "user": "Someone 2",
        "link": "link2"
      },
      {
        "user": "Someone 3",
        "link": "link3"
      }, {
        "user": "Someone 4",
        "link": "link4"
      }
    ],
    "name": "SOMETHING",
    "author": "someone",
    "id": 12345678,
    "vid": "link",
    "iframe": "link"

  }]
}

$('#levelname').text(list.list1[0].name);
$('#levelauthor').text(list.list1[0].author);
$('#levelid').text("ID: " + list.list1[0].id);
$('iframe1').attr('src', list.list1[0].iframe);

var html = "";
//loop through records json array
$.each(list.list1[0].records, function(i, vv) {
  //if `i` value is even one class or other.. 
  var class_name = i % 2 == 0 ? "recordd2" : "recordd"
  //append link and user name
  html += `<p class="${class_name}" onclick="window.open('${vv.link}')">${vv.user}</p>
   `
})

$(".records").append(html) //append p in div
.recordd2 {
  background-color: blue;
}

.recordd {
  background-color: pink;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="bg5">

  <p class="title" id="levelname"></p>
  <a href="../2">
    <p class="ttitle"> ></p>
  </a><br>
  <p class="subtitle" id="levelauthor"></p>

  <iframe class="vid" id="iframe1" frameborder="0" allowfullscreen></iframe><br><br><br>>

  <center>
    <h1 class="subtitle" id="levelid"></h1>
  </center>
  <div class="records">
    <h1 class="title2">RECORDS</h1>
    <!--p tag will come here-->
  </div>
</div>

Related