1

I like add text and some images to a div using jquery.

I can do this to add text:

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

$("#help").text('This is the description').css({"border": "1px solid", "color": "Black", "font-size": "14px", "font-weight": "bold", "padding": "30px 0 30px 0", "width": "auto", "height": "auto", "text-align": "left", "background": "lightyellow"});

I tried this:

$("#help").append('<img src="description.png" alt="box_plot_description" />'); 

});

How would append image to this text using jquery?

I have added the append after text but, append is overwriting the div. I like have text at the beginning of the div and after that an image.

1
  • 1
    see @Shyju's answer. You need to use .html() instead of .text() Commented Aug 9, 2012 at 13:46

3 Answers 3

5

use html() method

$("#help").html('<img src="somesrc.jpg" alt="someimage" />');

Working sample : http://jsfiddle.net/HdFqr/3/

If you want to keep the existing content and append the image, use append method

$("#help").text("Some Text");
$("#help").append('<img src="somesrc.jpg" alt="someimage" />');

Sample : http://jsfiddle.net/HdFqr/4/

You can also use appendTo method as well

var img=$("<img />").attr("src","someimag.jpg");
img.appendTo($("#help"));

Sample : http://jsfiddle.net/HdFqr/6/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your response. This overwrites my div, I like to append to my div. Basically, I look to put some description in text and then add an image.
2

If you want to append:

$('#divid').append('<img src="path" />');

If you just want to set a specific image:

$('#divid').html('<img src="path" />');

2 Comments

Everlof, I have update the code. When I do append after text, it overrites my div. I dont see the text in the div anymore.
Well, it depends how do you want it to look? This example uses p tags for the text jsfiddle.net/HdFqr/10
1

You can insert an image with .html()

$("#help").html('<img src="description.png" alt="box_plot_description" />'); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.