Jump to content

Need help with document.getElementById for multiple items ?


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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...