1

So I usually only deal with server side code, and need some assistance. in c# i was able to just do something like,

numStirng = listItem["fieldName"].toString();
num = Convert.ToInt(num);
listItem["fieldName"] = num + 1;

heres my javascript code

 var selectedList = web.get_lists().getByTitle('Easy Poll');
        context.load(selectedList);
            //var oListItem = selectedList.getItemById(parseInt("1", 10));
        var selectedItem = selectedList.getItemById(1);
                //selectedItem.get_item("EmployeeName", newName);

        selection = $('input[@name="food"]:checked').val();
        if(selection == "rubys")
        {
             num = selectedItem[rubys];
            selectedItem[rubys] = num + 1;
        }
        if(selection == "panda")
        {
             num = selectedItem[panda];
            selectedItem[panda] = num + 1;
        }
        if(selection == "subway")
        {
             num = selectedItem[subway];
            selectedItem[subway] = num + 1;
        }
        if(selection == "carls")
        {
             num = selectedItem[carls];
            selectedItem[carls] = num + 1;
        }

    selectedItem.update();
    //selectedList.update();
    context.load(selectedItem);
    context.executeQueryAsync();

so my questions becoem a)what am I doing wrong? b)is there a good direction i can look to or is this an easy fix, ty.

2

From your code, I can see that you are trying to add/update new item.

Coding with the JavaScript Client Object Model can be tricky at first if you are coming from a C# background. Keep in mind that you have to "bring" objects to the client side before you can do your operations on them. This "bringing objects to the client side" is done by the ClientContext.load() and ClientContext.executeQueryAsync() methods.

Also, since operations in JavaScript are async, you have to make heavy use of call back functions.

Here are few basic operations with the JavaScript CSOM which should get your started: http://msdn.microsoft.com/en-us/library/hh185011(v=office.14).aspx

Make sure to check all the How-To links on the left side.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.