Javascript if statement to insert an image -
i'm new javascript , trying replicate on spreadsheet, in javascript on webpage, i've got work in long winded way, looking ways simplify it.
the script embedded within html
<td class="td_col11csize"> <div class="tabletext"> <span class = "sub0a"> <script type="text/javascript"> if (game2player4sub == "") document.write(game2player4name, game2player4off) </script> </span> <span class = "sub1a"> <script type="text/javascript"> if (game2player4sub == "sub 1") document.write(game2player4name, game2player4off) </script> </span> <span class = "sub2a"> <script type="text/javascript"> if (game2player4sub == "sub 2") document.write(game2player4name, game2player4off) </script> </span> <span class = "sub3a"> <script type="text/javascript"> if (game2player4sub == "sub 3") document.write(game2player4name, game2player4off) </script> </span> <span class = "sub4a"> <script type="text/javascript"> if (game2player4sub == "sub 4") document.write(game2player4name, game2player4off) </script> </span> <span class = "sub5a"> <script type="text/javascript"> if (game2player4sub == "sub 5") document.write(game2player4name, game2player4off) </script> </span> <span class = "sub6a"> <script type="text/javascript"> if (game2player4sub == "sub 6") document.write(game2player4name, game2player4off) </script> </span> <span class = "sub7a"> <script type="text/javascript"> if (game2player4sub == "sub 7") document.write(game2player4name, game2player4off) </script> </span> </div> </td>
the code checks 8 conditions set font colour , add player's name , time taken off in brackets. above works, long winded , need @ writting function replace used 18 x 46 times on page.
i need add if statement add <img src ="yellowcard.png">
when (game2player4yellow == "n")
you're using js if php...
try like:
function getgame2playerdata(game2player4sub, game2player4yellow = null) { selector = game2player4sub.replace(" ","") + 'a'; elements = getelementsbyclassname(selector); img = ''; if (game2player4yellow == 'n') { img = '<img src ="yellowcard.png">'; } elements[0].innerhtml = img + game2player4name + ' ' + game2player4off; }
Comments
Post a Comment