0

My javascript dont seem to like the var divTag = '#' + ratinglistid + " > select[name=SelectedValue] > option:selected";

I couldn't find the answer searching.. Sorry for the messed up formatting, couldn't get it to go away.

 Script:

<script>
    function doSubmit(form, myRating, ratinglistid){
        var divTag = '#' + ratinglistid + " > select[name=SelectedValue] > option:selected";
        var ratingValue = $(divTag);
        $.ajax({
            type: "GET",
            url: "/RatingLists/Done",
            data: ({
                myrating: ratingValue,
                myRatingListId: ratinglistid
            }),
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        });
    }
</script>

Html I want to get value from:

    <div id='2'>
    <input type="hidden" name="RatingListId" value="2"/>
    <select id="SelectedValue" name="SelectedValue" onchange="doSubmit($(this).parents(&#39;form&#39;),2);"><option value="1">Inget intresse, tas bort</option>
    <option value="2">Svagt intresse, sparas</option>
    <option selected="selected" value="3">Intresserad-ingen kontakt &#228;n</option>
    <option value="4">4-Intreserad-vill ha kontakt</option>
    <option value="5">5-Vill byta!</option>
    <option value="100">6-Avvaktar v&#228;rdar</option>
    </select>
    </div
gdoron
  • 147,333
  • 58
  • 291
  • 367
Magnus Karlsson
  • 3,549
  • 3
  • 31
  • 57

2 Answers2

0

You are trying to pass a jQuery object to be serialized in the ajax function which causes the Illegal invocation error, use the .val() to get the value of the form element.

data: ({
    myrating: ratingValue.val(),
    myRatingListId: ratinglistid
}),
Musa
  • 96,336
  • 17
  • 118
  • 137
  • You had the right solution and if you update your answer I will give you the points back. For clarity my code snippet is the "right answer" until you do. – Magnus Karlsson Nov 28 '12 at 17:00
  • @MagnusKarlsson I would hardly call your answer clearer or clear for that matter. I admit my answer could be more in depth but since this question is a duplicate, [1](http://stackoverflow.com/questions/11729442/post-throwing-illegal-invocation) [2](http://stackoverflow.com/questions/13459623/illegal-invocation-jquery) [3](http://stackoverflow.com/questions/10232811/illegal-invocation-error) [4](http://stackoverflow.com/questions/10324594/jquery-illegal-invocation) [.](http://stackoverflow.com/questions/9048358/illegal-invocation-error-in-ajax-jquery-1-7-1) . ., I couldn't be bothered. – Musa Nov 28 '12 at 18:15
0

It could not evaluate inside get call so I had to do it like this:

var divTag = '#' + ratinglistid + " > select[name=SelectedValue] > option:selected";
var ratingValue = $(divTag).val();
    $.ajax({
                type: "GET",
                url: "/RatingLists/Done",
                data: ({
                    myrating: ratingValue,
                    RatingListId: ratinglistid
                }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess(ratinglistid),
                error: function () {
                    var id = ratinglistid;
                    $('#' + id + "tbody td").hover(function () {
                        $(this).parents('tr').addClass('highlight');
                    }, function () {
                        $(this).parents('tr').removeClass('highlight');
                    });
                }
            });
Magnus Karlsson
  • 3,549
  • 3
  • 31
  • 57