jQuery - Injecting HTML string into a div element

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

Who is Participating?
Try :

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


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

Open in new window

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.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
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.

All Courses

From novice to tech pro — start learning today.