Solved

placing image in table cell with jquery

Posted on 2016-10-02
5
42 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
  • 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 51

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 51

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 51

Expert Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now