Solved

placing image in table cell with jquery

Posted on 2016-10-02
5
75 Views
Last Modified: 2016-10-04
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?
0
Comment
Question by:maqskywalker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 2

Accepted Solution

by:
Amita Singh earned 250 total points
ID: 41825882
<script type="text/javascript">
        $(document).ready(function () {
			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 #row1 .col4').append(img);
            
        });
    </script>

Open in new window

0
 
LVL 57

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 41826070
A pure jQuery solution that does not require each row has an id or column has a class.
$(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:nth-child(4)').append(img);
});

Open in new window

0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41826072
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

0
 
LVL 1

Author Closing Comment

by:maqskywalker
ID: 41827877
thanks. both are great solutions.
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41827932
You are welcome.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET - Why is accordion not working? 3 72
javascript and jquery question 4 46
Time Picker 3 22
Can't see json data in ajax call to Highcharts 2 30
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question