Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
THANK YOU, this helps so much –  will Mar 5 '13 at 18:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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