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

EG:

<ul>
<li>Bananas</li>
<li>People</li>
<li>TV</li>
<li>Cheese</li>
<li>Cinema</li>
</ul>

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,

Luke
// 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;
		
		$.ajax({
			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

LVL 2
stellaartoisAsked:
Who is Participating?
 
leakim971PluritechnicianCommented:
Try :


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

or 


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

Open in new window

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