jQuery - Injecting HTML string into a div element

Posted on 2011-04-21
Medium Priority
Last Modified: 2012-06-27
Hello all,

I'm just wondering what the best way is to add and show a chunk of HTML to a div which already exists on the page.

I have a div called "result_list", as the user types a list of results will be returned from search.php



I want to inject this in to the div element every time they type in the box.

It works so far, I just want to know how to inject/update the HTML in the div

Thanks for assistance,

// jQuery code so far
	// Handle search result from top menu
	$('#search_term').bind("keyup", function(e) {
		// Set the values
		var search_term = $('#search_term').val();
		// Set the data string to be sent to the script
		dataString = "search_term=" + search_term;
			type: "POST",
			url: "ajax/search.php",
			data: dataString, 
			dataType: "html",
			success: function(search_return) {
				$('#result_list').// inject html to existing div

			error: function() {
				alert("not successful");

// Already existing div element
<div class="result_list"></div>

Open in new window

Question by:stellaartois
LVL 83

Accepted Solution

leakim971 earned 500 total points
ID: 35442258
Try :

$('#result_list').html(search_return); replace current content


$('#result_list').append(search_return); // add

Open in new window


Assisted Solution

myogesh_mathur earned 500 total points
ID: 35472890
Well first thing you need to do is set id element for your div like :-

<div class="result_list" id = "result_list"></div>

Open in new window

and then you can use the solution leakim971 given above,

Hope this helps.
LVL 53

Expert Comment

ID: 35872646
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

807 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