jQuery - Injecting HTML string into a div element

Posted on 2011-04-21
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 81

    Accepted Solution

    Try :

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

    Open in new window

    LVL 1

    Assisted Solution

    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

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

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    mapBully challenge 6 53
    Summernote required 3 21
    jquery not selector 4 10
    Page not loading properly 15 22
    In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now