LCJ
LCJ

Reputation: 22652

Intermittent error in jQuery Code

I am getting the following java script error (intermittently) ”return” statement outside of function.

Following is the function that has return in my jQuery code. I am wondering what is wrong in this code pattern? Is the call of another function in one function a problem? Any thoughts?

CODE

function getSelectedParameters(parameterControl) {
    var selectedElements = $.map($(parameterControl + " option:selected"), function (el, i) {
        var code = getCode($(el).text());
        return code;
    });
    var parameter = selectedElements.join(",");
    if (isAnyParameterSelected == 0) {
        if (parameter.length > 0) {
            isAnyParameterSelected = 1;
        }
    }
    return parameter;
}
function getCode(selectedValue) {
    var firstIndex = selectedValue.lastIndexOf(":");
    var code = selectedValue.substring(0, firstIndex);
    return code;
}

Upvotes: 1

Views: 87

Answers (4)

LCJ
LCJ

Reputation: 22652

Thanks to all the answers...

The error is not coming when I removed another script - href="javascript:return void(0);". I guess that was the problem

From the folllowing:

  <a href="javascript:return void(0);" id="MoveSelectedItems" class="k-button">
    <img src="~/Images/move.png" height="18" width="18" /> 
    Move 
  </a>

Upvotes: 0

sksallaj
sksallaj

Reputation: 4010

I really don't see anything wrong with your syntax, but there can always be some kind of weird underlying problem behind this. Maybe this approach will work, as I'm removing a return value from your inner anonymous function, because that is probably what is conflicting your return statement. I had a similar problem like this a year ago, and below is a workaround you could use.

function getSelectedParameters(parameterControl) {
    var selectedElements = new Array();
    $.map($(parameterControl + " option:selected"), function (el, i) {
        selectedElements = getCode($(el).text());
        return false; //edit this to make it false
    });
    var parameter = selectedElements.join(",");
    if (isAnyParameterSelected == 0) {
        if (parameter.length > 0) {
            isAnyParameterSelected = 1;
        }
    }
    return parameter;
}
function getCode(selectedValue) {
    var firstIndex = selectedValue.lastIndexOf(":");
    var code = selectedValue.substring(0, firstIndex);
    return code;
}

What I'm doing is removing the return from the $.map value, make it return as false; while at the same time, within the $.map it assigns to the outer variable selectedElements.

Maybe this will help or guide you in the right direction. I'm a little iffy on this code:

selectedElements = getCode($(el).text());

maybe you could do a push method suggested by SR5:

$.each($(parameterControl + " option:selected"), function (el) {
    selectedElements.push(getCode($(el).text()));
});

I think you're trying to create a new array out of this? I wish I had more to work with here..

If you do upvote my answer and used SR5's push method with $.each, please upvote SR5 as well :-p

Upvotes: 1

The Alpha
The Alpha

Reputation: 146191

This is not an answer to your question because the problem is in somewhere else according to the given code in your question but for whatever purpose the code you've written (two functions), that could be done within this one function and there are other ways too but this is enough, IMO.

function getSelected(el)
{
    var opt = $(el).find('option:selected'), l = opt.length, i = 0, arr = [], t;
    for(;i < l; i++) {
        t = $(opts[i]).text(), arr[i] = t.substring(0, t.lastIndexOf(":"));
    }
    isAnyParameterSelected = (isAnyParameterSelected == 0 && opt) ? 1 : 0;
    return arr.join(',');
}

Make sure that, the variable isAnyParameterSelected is available in the function execution scope. You can call the function like :

getSelected('.sel') // A select with class 'sel'

Check this fiddle for some examples using three versions of same function but one function (less code).

Upvotes: 1

SR5
SR5

Reputation: 95

Try this:
function getSelectedParameters(parameterControl) {
    var selectedElements = [];
    $.each($(parameterControl + " option:selected"), function (el) {
        selectedElements.push(getCode($(el).text()));
    });
    if (isAnyParameterSelected == 0 && selectedElements.length > 0) {
       isAnyParameterSelected = 1;
    }
    return selectedElements.join(",");
}

Upvotes: 1

Related Questions