Avatar of maqskywalker
maqskywalker
 asked on

placing image in table cell with jquery

Hi experts,

This is my code so far
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
  
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>

    <style type="text/css">
        #Table1 {
            width: 800px;
        }

        #row1 {
            height: 50px;
        }
        #row2 {
            height: 50px;
        }
        #row3 {
            height: 50px;
        }
        #row4 {
            height: 50px;
        }
        .col1 {
            width: 200px;
            font-family: Arial;
            font-weight: bold;
            font-size: 12px;
            border: 1px solid #b200ff;
            vertical-align: top;
        }
        .col2 {
            width: 200px;
            font-family: Arial;
            font-weight: bold;
            font-size: 12px;
            color: #006600;
            border: 1px solid #006600;
            vertical-align: top;
        }
        .col3 {
            width: 200px;
            font-family: Arial;
            font-weight: bold;
            font-size: 12px;
            color: #006600;
            border: 1px solid #123eeb;
        }
        .col4 {
            width: 200px;
            font-family: Arial;
            font-weight: bold;
            font-size: 12px;
            color: #006600;
            border: 1px solid #0094ff;
        }
    </style>

    <script type="text/javascript">
        $(document).ready(function () {
            // -------------------
                                        

            // ---- On Page Load ---- 

            // http://stackoverflow.com/questions/28552516/append-image-to-td-via-jquery
            
            var img = document.createElement("img");
            img.src = "http://media.idownloadblog.com/wp-content/uploads/2014/12/Google-Drive-3.4-for-iOS-app-icon-small.png";

            $('#Table1').find(tbody).append("<td>" + img + "</td>");

            
            // ---- On Page Load ---- 
            

            // -------------------
        });
    </script>

</head>
<body>

        <div>
            <table id="Table1">
                <tr id="row1">                    
                    <td class="col1"></td>
                    <td class="col2"></td>        
                    <td class="col3"></td>  
                    <td class="col4"></td>      
                </tr>
                <tr id="row2">                    
                    <td class="col1"></td>
                    <td class="col2"></td>        
                    <td class="col3"></td>  
                    <td class="col4"></td>                                                      
                </tr>
                <tr id="row3">                    
                    <td class="col1"></td>
                    <td class="col2"></td>        
                    <td class="col3"></td>  
                    <td class="col4"></td>                                                      
                </tr>
                <tr id="row4">                    
                    <td class="col1"></td>
                    <td class="col2"></td>        
                    <td class="col3"></td>  
                    <td class="col4"></td>                                                      
                </tr>
            </table>
            <br />

        </div>

</body>
</html>

Open in new window


My code is incorrect. Its not working.

What i want is....
Using jQuery, On page load, place the image inside col4 of row1 of the table with id of Table1

Anyone know the syntax for that?
jQuery

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Amita Singh

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

Alternative - last-child instead of nth-child
$(function() {
  var img = $('<img/>', {
    src: "http://media.idownloadblog.com/wp-content/uploads/2014/12/Google-Drive-3.4-for-iOS-app-icon-small.png"
  });
  // Independent of ID's and classes
  $('#Table1 tr:first-child td:last-child').append(img);
});

Open in new window

maqskywalker

ASKER
thanks. both are great solutions.
Julian Hansen

You are welcome.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck