0

I need to modify this function:

function checkPermissions(fid, obj) {

$("#permissions_"+fid+"_canview").attr("checked", obj.checked);

}

if the param "n" is "true" then instead of #permissions it'll output #permissionsSet.

is that possible?

1
  • 1
    Are you familiar with the if statement? Commented Sep 26, 2011 at 17:13

2 Answers 2

1

OP clarified the question to be change this whether or not a 3rd parameter was provided. Here you go.

function checkPermissions(fid, obj, n) {
  var id = n !== undefined ? '#permissionsSet' : '#permissions';
  $(id + fid + "_canview").attr("checked", obj.checked);
}

Note: This function can be freely used with passing 2 or 3 parameters (or really any number).

checkPermissions(1, this);        // Ok n === undefined 
checkPermissions(1, this, true);  // Ok n === true
checkPermissions(1);              // Okish n and obj === undefined
Sign up to request clarification or add additional context in comments.

4 Comments

I mean , if the 'n' is defined in the function callback fe. checkPermissions(1, this, true) then the 3th param is a 'n' but I need also to use the same function at the same time without the n param : checkPermission(1, this)
Aight, now when I check my checkbox, firebug says that the checkPermission function is undefined. My onClick callback: onClick="checkPermissions('.$gid.', this);"
@Lucas, this is likely a separate issue due to the ordering of javascript code after the definition of the onClick handler. You need to make sure the function is defined before the element to which you attach the handler
Thank you JaredPar, I got this working now. Thank you once again.
1
function checkPermissions(fid, obj, n) {

    $("#permissions" + ((n) ? "Set" : "") + "_"+fid+"_canview").attr("checked", obj.checked);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.