I have a list of items on a webpage. I would like to load a form via Ajax each time I click on any item in the list. The HTML looks like this:
<div class="item">
<... some html code for the first item ...>
</div>
</div class="item">
<... some html code for the next item ...>
</div>
etc..
My goal is that if the user clicks on the first “item-div” a form shall be loaded using AJAX. The loaded form shall be inserted after the “item-div”… something like this:
<div class="item">
<... some html code for the first item ...>
</div>
<form>
<... some html code for the form... loaded via AJAX...>
</form>
</div class="item">
<... some html code for the next item ...>
</div>
etc..
I can’t understand why this won’t work. Here is my jQuery code:
$(document).ready(function(){
$(".item").click(function(){
$(this).after().load("loadform.php");
});
});
The Ajax-load function overwrites the inner <... some html code... >
instead of adding it after the “item-div”. What is wrong? How can I solve it?
4
Answers
Maybe Try the below way. Get the data from php and then insert it using after function.
You can not use load() with after() . When a successful response is detected (i.e. when textStatus is “success” or “notmodified”), .load() sets the HTML contents of the matched elements to the returned data.If you want to load content fro loadform you can try $.get method as below.
Sometime it happens like, if you have inserted new html code into DOM with jquery, then you will follow this procedure. This will refresh the DOM behind the seen. It may help you, A simple test without ajax. You have error in your ajax request, first be sure to
console.log(data)
your return data, and then append it. Thanks.A Simple JSFiddle Test
I have no dummy snippet somewhere in the internet where I can load some dummy content to simulate your
loadform.php
but I tested it locally and it is working.on('click', function ...)
currentItem
$.get
currentItem