tags:

views:

25

answers:

2
function manageVoting() {
    $("div.votemaincontainer").each(function () {
        // variable Declaration and intialization.
        var myRating = parseInt($(this).find('#[id$=hfMyVote]').val());
        var currentVote = parseInt($(this).find('#[id$=hfCurrentVote]').val());
        $('.VoteDownImage').live(click, function () {
            ajaxcall(0);
        });
        $('.voteupImage').live(click, function () {
            ajaxcall(1);
        });
    }

    function ajaxcall(a){ // here it's giving error.
        var questionID = parseInt($(this).find('#[id$=currentquestionId]').val());
        var objectType = 100;
        var previousvote = 2;
        $.ajax({
            url: 'UserControls/Vote/VoteAction.aspx/Voting',
            cache: false,
            type: 'POST',
            data: '{ObjectType: "' + objectType + '", guid: "' + currentGuid + '",previousVote: "' + previousvote + '",vote:"' + a + '"}',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (data) {
                var result = eval(data.d);
            }
        });
    }
    $(function () {
        //    manageVoting();
    });
+4  A: 

EDIT: After improving the code indentation in your question, it appears as though you forgot to close the .each() loop with }); before the closing bracket for the manageVoting() function.

Proper code indentation is extremely helpful.

Also, it doesn't make too much sense to apply .live() in a loop. You only need to do that once.

Here's your code with the improvements:

  // Call these once on page load.
  // Note the difference in capitalization in your selectors. Make sure it is
  //    the same in your HTML classes
$('.VoteDownImage').live(click, function () {
    ajaxcall(0);
});
$('.voteupImage').live(click, function () {
    ajaxcall(1);
});

function manageVoting() {
    $("div.votemaincontainer").each(function () {
        // variable Declaration and intialization.
        var myRating = parseInt($(this).find('#[id$=hfMyVote]').val());
        var currentVote = parseInt($(this).find('#[id$=hfCurrentVote]').val());
    });  // closed the .each() loop
}

function ajaxcall(a){ // here it's giving error.
    var questionID = parseInt($(this).find('#[id$=currentquestionId]').val());
    var objectType = 100;
    var previousvote = 2;
    $.ajax({
        url: 'UserControls/Vote/VoteAction.aspx/Voting',
        cache: false,
        type: 'POST',
        data: '{ObjectType: "' + objectType + '", guid: "' + currentGuid + '",previousVote: "' + previousvote + '",vote:"' + a + '"}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (data) {
            var result = eval(data.d);
        }
    });
}
$(function () {
    //    manageVoting();
});

Original answer:

You don't specify a type for function parameters (or any other variables) in javascript. This is because of loose typing, where any variable can be used to store any type.

This line:

function ajaxcall(int a) 

should be:

function ajaxcall(a) 
patrick dw
sir can u check ones my code..
Nishant
@Nishant - Can you provide more information? What is the error?
patrick dw
+1  A: 

@Nishant - when you get done unwinding your code there are a lot of problems.

From looking at your code you are trying to have up/down voting on images.

Well I have some questions and statements:

  1. What are the local variable in manageVoting used for?
  2. Why do you have variables starting with uppercase and some with lowercase?
  3. The this reference in parseInt($(this).find('#[id$=currentquestionId]').val()) is not what you are looking for in getting the id.
  4. Try renaming your function to something other than ajaxCall.
  5. Don't build your own query string let JavaScritp and jQuery handle that.

Here is a start with your code:

function updateVote(elementClicking, vote){ // here it's giving error.
     var imagVote = {
         ObjectType: 100,
         guid: parseInt($(elementClicking).find('#[id$=currentquestionId]').val()),
         previousVote: 2,
         vote: vote
     } ;

     $.ajax({
         url: 'UserControls/Vote/VoteAction.aspx/Voting',
         cache: false,
         type: 'POST',
         data: imagVote,
         contentType: 'application/json; charset=utf-8',
         dataType: 'json',
         success: function (data) {
             var result = eval(data.d);
         }
     });
 }

$('.VoteDownImage').live(click, function () {
    updateVote(this, 0);
});

$('.voteupImage').live(click, function () {
    updateVote(this, 1);
});

function manageVoting() {
    $("div.votemaincontainer").each(function () {
        // variable Declaration and intialization.
        var myRating = parseInt($(this).find('#[id$=hfMyVote]').val());
        var currentVote = parseInt($(this).find('#[id$=hfCurrentVote]').val());
    });

 }

$(function () {
    manageVoting();
});

One last piece of unwanted advice. Don't make your code more complicated than it has to be## Heading ##

Gutzofter