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

I'm trying to prevent users from editing an item after i've set a "WIP cutoff". I have a checkbox to set this and I'm trying to get the status of that checkbox using a function that has been used extensively in an existing customised sharepoint 2007 site. The Function is:

function getField(fieldType,fieldTitle) { 
    var docTags = document.getElementsByTagName(fieldType); 
    for (var i=0; i < docTags.length; i++) { 
        if (docTags[i].title == fieldTitle) { 
            return docTags[i] 
        } 
    } 
}

So in order to get the "Checked Status" of the box:

<input name="ctl00$m$g_0c239981_ad37_4fda_8a14_7015542b4518$ff113_1$ctl00$ctl00$BooleanField" title="WIP Closed Off" id="ctl00_m_g_0c239981_ad37_4fda_8a14_7015542b4518_ff113_1_ctl00_ctl00_BooleanField" type="checkbox" CHECKED="checked" value="on"/>

I'm using with the help of share points built-in "PreSaveAction" function [so I've been told]:

// PreSaves Edit WIP NEW
function PreSaveAction() {
    if (getField('input', 'WIP Closed Off').CHECKED == "checked") {
        alert("This Campaign has been closed, you can not edit products.");
        //return false;
        history.go(-1);
    }
}

I originally thought that I should be getting the "Value" of the checkbox but it is alway ON regardless of the checkbox state.

Any help appreciated.

share|improve this question

2 Answers 2

I believe the DOM will hold the checked property as lower case, so you should be fine by doing:

if (getField('input', 'WIP Closed Off').checked) {...}

Make sure that getField actually return the element you seek.
Another approach is to use jQuery, like this:

if (!$('[title=WIP Closed Off]').prop('checked'))) return;
share|improve this answer
    
maybe slightly out of context, but after jquery introduced .prop it's better to use that for this kind of case, like - if (!$('[title=WIP Closed Off]').prop('checked')), anyway performance is same, just another approach –  Diptarag Oct 12 '12 at 5:45
    
Good call, updating answer. Also, in theory it might actually be faster for those that care, there are some tests as well. I do find .is to be readable though, and it's even 1 char shorter :D –  eirikb Oct 12 '12 at 5:55
    
Hi Eirikb, thanks for the tip, the checked property was indeed lowercase, so I've gotten a step closer. Unfortunately Jquery is a bit beyond me at this stage. So I've got this bit of code now: codefunction PreSaveAction() {alert(getField('input', 'WIP Closed Off').checked); if (getField('input', 'WIP Closed Off').checked == "true") {alert("This Campaign has been closed, you can not edit products."); history.go(-1);}code The first alert returns "true", however the if statement doesn't happen–the record gets saved. I've tried doing additional OR statements for "on" and "checked" but nothing. –  imonkey Oct 16 '12 at 1:35
    
You seem to have answered your own question, but in any case, by using the first line of code I gave you it should work fine. It does not weakly compare the boolean value true to the string 'true', as true == 'true' is false. –  eirikb Oct 16 '12 at 4:21

True is not a string! In both cases where I was checking to see if checked was "checked" or "true" I was calling it as a string with the double quotes. I removed the quotes and the if statement worked.

Rookie mistake!

share|improve this answer

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.