I've been trying to convert this code to jQuery from Javascript but to no avail. This code is for adding a new item to a list which I took from the msdn website.
It works in javascript but I need it to run every time the page is loaded. I tried to google for why it wasn't working with javascript and found this. The guy who asked the question said it worked well with jQuery. So, I've been trying to convert this.
All help is appreciated! Thanks!!
Note: As I'm working for a client, I'm not allowed to use third party tools like SPServices.
Edit: Code I've done till now:
<script language="javascript" src="/SiteAssets/jquery-1.8.3.min.js" type="text/javascript">
</script><script language="javascript" type="text/javascript">
function createListItem() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle("Announcements");
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item("Title", "My New Item!");
oListItem.set_item("Body", "Hello World!");
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
alert("Item created: " + oListItem.get_id());
}
function onQueryFailed(sender, args) {
alert("Request failed. " + args.get_message() + "\n" + args.get_stackTrace());
}
$(document).ready(function()
{
createListItem();
});</script>