Solved

placing image in table cell with jquery

Posted on 2016-10-02
5
70 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 56

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 56

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 56

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
Generate Numbers in JQuery file 11 72
jQuery on Submit 4 53
Character counter breaks after adding EmojiOne Area 4 49
setInterval() calls function twice in one interval 2 36
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…
Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
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)

749 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