Help using jQuery to replace table cell contents with image

I'd like to replace the third cell of a table row with an image.  Below is what my table looks like.  I also have the code below but it's not working.

<table summary="Rating Scale Question">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>

<script type="text/javascript">
$(document).ready(function () {
	$('table[summary='Rating Scale Question'] tr:first-child td:nth-child(2)').each(function () {
          $(this).html('<img src="path-to-my-images/smiley_poor.png" alt="Poor" />');
    }
}
</script>

Open in new window

greddinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

greddinAuthor Commented:
Just to clarify.  I want to replace the contents of the table cell and not the element itself.

The final output would be something like this:

<td><img src="my image.png"></td>

Thanks.
0
Chris StanyonWebDevCommented:
You've got a slight issue with your single quotes. You also don't need to each it or call first-child on the <tr>. Finally nth-child is one-based, not zero-based, so the third column is 3

$('table[summary="Rating Scale Question"] tr td:nth-child(3)')
    .html('<img src="path-to-my-images/smiley_poor.png" alt="Poor">');

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris StanyonWebDevCommented:
Actually, if you only want to change the 3rd TD in the first row, then you would need first-child on the tr. My previous code assumed you wanted to set the content for the 3rd TD in each row.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

greddinAuthor Commented:
Thanks, so I've got this now but it's not working.  Am I missing a parenthesis somewhere?

<script type="text/javascript">
$(document).ready(function () {
$('table[summary="Rating Scale Question"] tr td:nth-child(3)').html('<img src="https://f001.backblazeb2.com/file/greg-hp-public/images/smiley_poor.png" alt="Poor">');
}
</script>

Open in new window

0
greddinAuthor Commented:
Nevermind, I was missing the closing  "});".
0
greddinAuthor Commented:
Thanks Chris!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.