javascript - How to post a search form using jQuery/Ajax and put results in a div? -
i'm trying post search query local server , return results div using ajax. i'm following formula documentation cannot output. not getting console log errors. source code from: post form using ajax , put results in div
<form action="/" id="searchform"> <input type="text" name="s" placeholder="search..."> <input type="submit" value="search"> </form> <!-- result of search rendered inside div --> <div id="result"></div> <p id="content"> lorem ipsum dummy text of printing , typesetting industry.lorem ipsum has been industry's standard dummy text ever since 1500s, when unknown printer took galley of type , scrambled make type specimen book. </p> <script> $(document).ready(function () { $("#searchform").submit(function(event) { // stop form submitting event.preventdefault(); // values elements on page: var $form = $(this), term = $form.find( "input[name='s']").val(), url = $form.attr("action"); // send data using post var posting = $.post( url, {s: term}); // put results in div posting.done(function(data) { var content = $(data).find("#content"); $("#result").empty().append(content); }); }); }); </script>
you can use html()
method, i.e.:
var content = data; console.log(data); $("#result").html(content);
Comments
Post a Comment