Nodnin
Nodnin

Reputation: 451

How can I populate an image in a table row?

I am trying to populate images into a table. It's like an image that would be a question and the other would be an image that would be an answer and they would be compared in a table. I tried to search online but could not find anything substantial.

<html>
  <head>
    <title>Game Selection</title>
    <script type="text/javascript">
      function gameddselected() {
        var gamedd = document.getElementById('gamedd');
        var x =document.getElementById('scoretable').rows
        var a=x[0].cells
        var b=x[1].cells
        var c=x[2].cells
        if (gamedd.value=='1') {
          a[0].innerHTML="Game1 Score Table"
        } else if (gamedd.value=='2') {
          a[0].innerHTML="Game2 Score Table"
        } else if (gamedd.value=='3') {
          a[0].innerHTML="Game3 Score Table"
        } else if (gamedd.value=='4') {
          a[0].innerHTML="Game4 Score Table"
        }
      }
    </script>
  </head>
  <body background="http://1.bp.blogspot.com/_68sk2PaFt3Q/Sl8n81IoRcI/AAAAAAAAEUw/AlnMPEC9uc0/s1600/Disable+File+Indexing+In+Windows++Speed+Up+Windows+By+Disabling+Search+Index+2.jpg" width="80%" height="80%">
    <h2 style="text-align: center;">
      <span style="font-size:18px;">
        <span style="font-family: arial, helvetica, sans-serif;">Please select a Game&nbsp;
        </span>
      </span>
    </h2>
    <form action="" method="POST" name="form2">
      <div align="center">Select:
        <select name="gamedd" id="gamedd" style="width:200px" onChange="gameddselected()">
          <option value="1">Game1</option><option value="2">Game2</option>
          <option value="3">Game3</option><option value="4">Game4</option>
        </select>
      </div>
    </form>
    <TABLE BORDER="5" WIDTH="50%" CELLPADDING="4" CELLSPACING="3" ALIGN="CENTER" id="scoretable">
      <TR>
        <TH COLSPAN="3"><BR><H3>TABLE TITLE</H3></TH>
      </TR>
      <TR>
        <TH>Question Images</TH>
        <TH>Answer Images</TH>
        <TH>Score</TH>
      </TR>
      <TR ALIGN="CENTER">
        <TD>Data 1</TD>
        <TD>Data 2</TD>
        <TD>Data 2</TD>
      </TR>
    </TABLE>
  </body>
</html>

I want an image to be produced in place of Data1 in the table. How can that be done?

Any help would be appreciated :)

Upvotes: 3

Views: 4182

Answers (2)

Amir Jalali
Amir Jalali

Reputation: 3212

HTML

<TABLE BORDER="5" WIDTH="50%" CELLPADDING="4" CELLSPACING="3" ALIGN="CENTER" id="scoretable">
  <TR>
    <TH COLSPAN="3"><BR><H3>TABLE TITLE</H3></TH>
  </TR>
  <TR>
    <TH>Question Images</TH>
    <TH>Answer Images</TH>
    <TH>Score</TH>
  </TR>
  <TR ALIGN="CENTER">
    <TD>Data 1</TD>
    <TD>Data 2</TD>
    <TD>Data 2</TD>
  </TR>
</TABLE>

CODE using jquery and pseudo code

$("#gamedd").change(function(){
    if selected == urCriteria then; 
    var img = $("<img />").attr('src', 'http://somedomain.com/image.jpg');
    $(table tr:lastchild td:firstchild).append(img );
 });

you can even load the image string url by using ajax call

Upvotes: 2

dda
dda

Reputation: 6203


HTML

  <TR ALIGN="CENTER">
    <TD id="Data1">Data 1</TD>
    <TD id="Data2">Data 2</TD>
    <TD id="Data3">Data 3</TD>
  </TR>

Code

for(i=1;i<4;i++) {
  x=document.getElementById("Data"+i);
  x.innerHTML="<img src='/path/to/image"+i+".jpg' />";
// Assuming your images are named [something]NUMBER.jpg
// You could instead use an array
}

RESULT

<div>​
  <td id=​"Data1">​<img src=​"/​path/​to/​image1.jpg">​</td>​
  <td id=​"Data2">​<img src=​"/​path/​to/​image2.jpg">​</td>​
  <td id=​"Data3">​<img src=​"/​path/​to/​image3.jpg">​</td>​
</div>​

Upvotes: 1

Related Questions