Make a Football Live Scoreboard App with JavaScript

 


Hello Devs,  In this new post I'm going to show you how to make a football live scoreboard app with HTML, CSS, and Javascript. it's a very cool project that you can add to your portfolio and amaze your future potential client.  For this project, we will need to use a Restful API to get all the data about the matches and the score. for more information visit the API documentation


first, we will start by creating the layout of our app using HTML and CSS

HTML

<!DOCTYPE HTML>


<html>
    <head>
        <title>Soccer Live Score</title>   
        <link rel="stylesheet" href="main.css">
    </head>
    
    <body>
        <div class="container">
            <h1>ScoreBoard</h1>
            <div class="title-box">
                <p>Local Team</p>
                <p id="elapsed">45'</p>
                <p>Visitor Team</p>
            </div>
            <div class="title-box">
                <div class="team">
                    <img  id="homeLogo" >
                    <p id="homeName">Team name</p>
                </div>
                <p id="goals">3  -  1</p>
                <div class="team">
                    <img id="awayLogo">
                    <p id="awayName">Team name</p>
                </div>
                 
            </div>
            <hr>
            <div id="matchTable" class="matches-table">

            </div>
        </div>
         <script src="main.js"></script>
    </body>
</html>


CSS: 

@import url('https://fonts.googleapis.com/css2?family=Roboto');

*{
    margin0;
    padding0;
    outline0;
    font-family'Roboto'sans-serif;
    text-aligncenter;
}

body{
    height100vh;
    background-imageurl(bg.jpg);
    background-sizecover;
    background-repeatno-repeat;
    background-positioncenter;
}

.container{
    positionabsolute;
    padding16px;
    top50%;
    left50%;
    transformtranslate(-50%,-50%);
    width50%;
    background-color:#FAFAFA;
    text-aligncenter;
    border-radius4px;
    text-transformuppercase;
    text-aligncenter;
}

.title-box{
    margin25px 0;
    displayflex;
    flex-directionrow;
    flex-wrapnowrap;
    justify-contentspace-around;
    align-itemscenter;
    width100%;
    font-size1.5em;
   
    
}
.title-box #goals{
    font-size1.8em;
}
.team{
    width100px;
}
.team img{
    height54px;
    width54px;
}
.matches-table{
    margin-top50px;
    displayflex;
    flex-directioncolumn;
}
.match-tile{
    positionrelative;
    left50%;
    transformtranslateX(-50%);
    margin10px 0;
    displayflex;
    flex-directionrow;
    justify-contentspace-around;
    align-itemscenter;
    
}
.match-tile img{
    width52px;
    height52px;
}
.match-tile p{
    font-size1.2rem;
}

.match-tile #goals{
    font-size1.8rem;
}
.match-tile .team{
    width100px;
}


And now let's add our javascript code, but before we do this, make sure to create a free account in football API and use your own API key just add as a value for the variable

//getting the DOM elements
var elapsedTime = document.querySelector("#elapsed");
var homeTeamImage = document.querySelector("#homeLogo");
var homeTeamName = document.querySelector("#homeName");
var awayTeamImage = document.querySelector("#awayLogo");
var awayTeamName = document.querySelector("#awayName");
var lastMatchGoal = document.querySelector("#goals");
var matchTable = document.querySelector("#matchTable");


//the functions to create an element
function addMatchTile(data){
    //createing the tile div
    var matchtile = document.createElement('div');
    matchtile.classList.add("match-tile");

    //creating the home match box
    var homeTeam = document.createElement('div');
    homeTeam.classList.add("team");
    //creating the image and the text
    var homeTileTeamName = document.createElement('p');
    homeTileTeamName.innerHTML = data['teams']['home']['name'];
    var homeTileTeamLogo = document.createElement('img');
    homeTileTeamLogo.src=data['teams']['home']['logo'];
    homeTeam.appendChild(homeTileTeamLogo);
    homeTeam.appendChild(homeTileTeamName);

    var awayTeam = document.createElement('div');
    awayTeam.classList.add("team");
    //creating the image and the text
    var awayTileTeamName = document.createElement('p');
    awayTileTeamName.innerHTML = data['teams']['away']['name'];
    var awayTileTeamLogo = document.createElement('img');
    awayTileTeamLogo.src=data['teams']['away']['logo'];
    awayTeam.appendChild(awayTileTeamLogo);
    awayTeam.appendChild(awayTileTeamName);

    //createing the score
    var score = document.createElement('p');
    score.innerHTML = data['goals']['home'] + " - " + data['goals']['away'];

    //append all the element to the parent
    matchtile.appendChild(homeTeam);
    matchtile.appendChild(score);
    matchtile.appendChild(awayTeam);

    matchTable.appendChild(matchtile);
}
//fetching the data
fetch("https://v3.football.api-sports.io/fixtures?live=all", {
    "method": "GET",
    "headers": {
        "x-rapidapi-host": "v3.football.api-sports.io",
        "x-rapidapi-key": "PLACE YOUR API KEY HERE"
    }
})
.then(response => response.json().then(data => {
    var matchesList = data['response'];
    var fixture = matchesList[0]['fixture'];
    var goals = matchesList[0]['goals'];
    var teams = matchesList[0]['teams'];
    console.log(matchesList.length);
   //Now let's set our first match
   elapsedTime.innerHTML = fixture['status']['elapsed'] + "'";
   homeTeamImage.src = teams['home']['logo'];
   homeTeamName.innerHTML = teams['home']['name'];
   awayTeamImage.src = teams['away']['logo'];
   awayTeamName.innerHTML = teams['away']['name'];
   lastMatchGoal.innerHTML = goals['home']+ " - " + goals['away'];

   for(var i = 1i<matchesList.length;i++){
       addMatchTile(matchesList[i]);
   }

}))
.catch(err => {
    console.log(err);
});



Make a Football Live Scoreboard App with JavaScript Make a Football Live Scoreboard App with JavaScript Reviewed by Medics on September 30, 2020 Rating: 5

20 comments:

  1. This blog is very helpfull information thank you so much for sharing this.

    How to make watch with HTML & CSS ?
    https://www.maxonpoll.online/2020/10/how-to-make-watch-with-html-css.html

    ReplyDelete
  2. Good evening, first I want to congratulate you for the Excellent Tutorial on the Creation of an Electronic Scoreboard updated in real time and I would like to take the opportunity to ask a question about this part of the code below that is in main.js

    //fetching the data
    fetch("https://v3.football.api-sports.io/fixtures?live=all", {
    "method": "GET",
    "headers": {
    "x-rapidapi-host": "v3.football.api-sports.io",
    "x-rapidapi-key": "PLACE YOUR API KEY HERE"
    }
    })
    .then(response => response.json().then(data => {
    var matchesList = data['response'];
    var fixture = matchesList[0]['fixture'];
    var goals = matchesList[0]['goals'];
    var teams = matchesList[0]['teams'];
    console.log(matchesList.length);
    //Now let's set our first match
    elapsedTime.innerHTML = fixture['status']['elapsed'] + "'";
    homeTeamImage.src = teams['home']['logo'];
    homeTeamName.innerHTML = teams['home']['name'];
    awayTeamImage.src = teams['away']['logo'];
    awayTeamName.innerHTML = teams['away']['name'];
    lastMatchGoal.innerHTML = goals['home']+ " - " + goals['away'];

    for(var i = 1; i<matchesList.length;i++){
    addMatchTile(matchesList[i]);
    }

    I would like to know how I do it instead of retrieving data from a web service ... retrieving data from a database which would provide us with customized championships and leaderboards..there's even a suggestion for the next video but how could I adapt this part of the code for a call to a table in a MySql database?

    Sincerely - Luiz Antonio - Brazil

    ReplyDelete
    Replies
    1. How do I know which function to call, element to create and etc if it were to be another API &key from another source?

      Delete
  3. Five-a-side football is the ultimate sport to get all your friends for a stag get-together and have a old-fashioned good time. football news

    ReplyDelete
  4. https://youtu.be/qMdsZYGWw2I

    Download free blogger script
    Vimeo thumbnail downloaded script

    ReplyDelete
  5. A lot of people having an incorrect image about the cash advance loans or sometimes refer it as bad credit payday loans. free football streaming websites

    ReplyDelete
  6. Wow, excellent post. I'd like to draft like this too - taking time and real hard work to make a great article. This post has encouraged me to write some posts that I am going to write soon. ผลบอล

    ReplyDelete
  7. Yes, I am entirely agreed with this article, and I just want say that this article is very helpful and enlightening. I also have some precious piece of concerned info !!!!!!Thanks. ผลบอลสด

    ReplyDelete
  8. thank you for the information sharing. Please also check my free livescore @ https://spbotop.com

    ReplyDelete
  9. Betting on football requires a lot of background research regarding the form of the players in the team and their physical status. norwich city news



    ReplyDelete
  10. This article gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice article. ดูบอลสด

    ReplyDelete
  11. How do I make it so I wont go over 100 requests/day. Btw great site, keep it up.

    ReplyDelete
  12. Hi All,

    Great video and thanks for sharing. I have encountered an interesting error which I'm unsure of. When running the main.js code as you have posted above with the live= all API (https://v3.football.api-sports.io/fixtures?live=all), everything seems to work as it should. However, when you try and customise the API and add in specific leagues, an error occurs. The API I am trying to implement as per the API documentation is the following:
    "https://v3.football.api-sports.io/fixtures?live=307"

    But when trying to view the live results of that league you get an error:
    TypeError: Cannot read properties of undefined (reading 'fixture')
    at scores.js:58:33

    How is it that when all fixtures that day are implemented this works fine but when you specify a league like I have, you encounter issues? I can paste full code snippet if anyone responds.

    ReplyDelete
    Replies
    1. Hi, I was reding your message but i don't know how to fix it. I'm doing a football Live Score but the team names doesn't show up on the score sheet, is it the same with you? Here is my code:

      var elapsedTime = document.querySelector("#elapsed");
      var homeTeamLogo = document.querySelector("#homeLogo");
      var homeTeamName = document.querySelector("#homeName");
      var awayTeamLogo = document.querySelector("#awayLogo");
      var awayTeamName = document.querySelector("#awayName");
      var lastMatchGoals = document.querySelector("#goals");
      var matchTable = document.querySelector("#matchTable");




      function addMatchTile(data){

      var matchTile = document.createElement('div');
      matchTile.classList.add("match-tile");


      //home team box
      var homeTeam = document.createElement('div');
      homeTeam.classList.add("team");
      //logo og texti við home
      var homeTileLogo = document.createElement('img');
      homeTileLogo.src = data['teams']['home']['logo'];
      var homeTileName = document.createElement('p');
      homeTeamName.innerHTML = data['teams']['home']['name'];

      homeTeam.appendChild(homeTileLogo);
      homeTeam.appendChild(homeTileName);

      //away team box
      var awayTeam = document.createElement('div');
      awayTeam.classList.add("team");
      //logo og texti við away
      var awayTileLogo = document.createElement('img');
      awayTeamName.innerHTML = data['teams']['away']['name'];
      var awayTileName = document.createElement('p');
      awayTileLogo.src = data['teams']['away']['logo'];

      awayTeam.appendChild(awayTileLogo);
      awayTeam.appendChild(awayTileName);



      //Búa til score
      var score = document.createElement('p');
      score.innerHTML = data['goals']['home'] + " - "+ data['goals']['away'];


      matchTile.appendChild(homeTeam);
      matchTile.appendChild(score);
      matchTile.appendChild(awayTeam);

      matchTable.appendChild(matchTile);
      }



      function getData(){
      //Hér kemur API og fleiri upplýsingar
      //https://api-football-v1.p.rapidapi.com/v3/timezone
      fetch("https://api-football-v1.p.rapidapi.com/v3/fixtures?live=all", {
      "method" : "Get",
      "headers" : {
      "x-rapidapi-host": "api-football-v1.p.rapidapi.com",
      "x-rapidapi-key": "my key",

      }

      })
      .then(response => response.json().then(data => {
      var matchesList = data['response'];
      var fixture = matchesList[0]['fixture'];
      var goals = matchesList[0]['goals'];
      var teams = matchesList[0]['teams'];
      console.log(matchesList.length);
      //Now let's set our first match
      elapsedTime.innerHTML = fixture['status']['elapsed'] + "'";
      homeTeamLogo.src = teams['home']['logo'];
      homeTeamName.innerHTML = teams['home']['name'];
      awayTeamLogo.src = teams['away']['logo'];
      awayTeamName.innerHTML = teams['away']['name'];
      lastMatchGoals.innerHTML = goals['home']+ " - " + goals['away'];


      for(var i = 1; i {
      console.log(err);
      });



      }

      getData();


      Delete
  13. how can i get api for all sports scores

    ReplyDelete
  14. im also having a problem with the fixtures, i keep getting this error: "TypeError: Cannot read properties of undefined (reading 'fixture')". How do i fix this?

    ReplyDelete

-->
Powered by Blogger.