Jump to content

Need help with document.getElementById for multiple items ?


vmars316

Recommended Posts

Hello & Thanks ,

var player1Score = 10;

var player2Score = 20;

document.getElementById('keepScore').innerHTML = ' player2Score = ' + player2Score;

 

I want to be able to display several items on one line with the above code or preferably something that works .

Pls , how can I do this .

 

Thanks

Link to comment
Share on other sites

Thanks ,

this is what i am trying to get to run :

document.getElementById('keepScore').innerHTML = ' player2Score = ' + player2Score;

but when I get this to work , i will have 4 players , player1 -player4 .

Link to comment
Share on other sites

I couldn't get the crazy thing working so I did this instead:

<div>	
<table style="width:100%">
  <tr>
    <td width="10%"> totalScore = </td> <td id="totalScore" width="5%"> </td>
    <td width="8%"></td>
    <td width="10%"> oops! = </td> <td id="oopsScore" width="5%"> </td>
    <td width="8%"></td>
    <td width="10%"> totalHits = </td> <td id="totalHits" width="5%"> </td>
    <td width="8%"></td>
    <td width="10%"> totalShots = </td> <td id="totalShots" width="5%"> </td>	
  </tr>
  </table>
</div>

Thanks

Link to comment
Share on other sites

Why not use array

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var items = [["player1Score", 10],["player2Score", 20]];
//to add extra item
items[2]=["player3Score", 30];
var join="";

for(i=0;i<items.length;i++){

join+= items[i][0]+"= "+items[i][1];
if(i<items.length-1){
join+=", ";
}
else
{
join+="."
}
}

document.getElementById("demo").innerHTML =join;

</script>

</body>
</html>

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...