4

I am working on a sharepoint server 2013. and inside my built-in create.aspx list form i added the following javascript code:-

var selectobject = document.getElementById("Approver_90afaad9-2b54-46fb-8ac1-7374e4efa11e_$DropDownChoice");

for (var i=selectobject.options.length-1; i>=0; i--)
    {

  selectobject.remove(i);
    }

now as shown in the code i am selecting a drop-down list based on its id, then i am removing its choices.

now i want to modify my javascript to use the SPUtility.js library to reference the columns using their internal names, rather than referencing them using their markup id. but i am not sure if SPUtility.js will support or provide the features to remove choices from my drop-down list as i am current doing inside my pure javascript code?

so if i replace this:-

var selectobject = document.getElementById("Approver_90afaad9-2b54-46fb-8ac1-7374e4efa11e_$DropDownChoice");

with SPUtility.js syntax:-

var selectobject = SPUtility.GetSPFieldByInternalName('Approver');

will i still be able to remove the choices??

  • Why don't you try the code? – harshal gite May 30 '17 at 12:47
  • You are halfway there, so indeed you could give it a shot of your own. Though, a pure JavaScript-solution (non-library dependent) can be implemented to get element by its content (e.g. divs[i].innerHTML.indexOf("Approver")). – moe May 30 '17 at 13:17
  • @moe i went with using SPUtility appraoch as it will provide the most standard way to select columns inside my create/edit forms. as i do not need to go through the markup which i do not own and which might change in the future if we update sharepoint. now i know that using SPUtility mean that i will add a dependency inside my sharepoint. but i will live with this drawback as it will allow me to say that i want to select a field inside my form which have ... – john Gu May 30 '17 at 13:41
  • ...for example the following internal name SPUtility.GetSPFieldByInternalName('Approver');.. but i am not sure if i will be able to add choices to the element which i select using SPUtility()? – john Gu May 30 '17 at 13:41
  • @harshalgite which code i should test ? – john Gu May 30 '17 at 13:41
3

Try accessing the DropDown property of the selectedObject. Haven't tried SPUtility library but looks like (after examining the code) that the HTML object is available in "DropDown" for choice fields.

Have a look at the code example here: https://github.com/kitmenke/sputility/blob/master/examples/dropdownchange.html

$(countryField.Dropdown) is used to get the dropdown control.

Once you access the html control you can do anything with it. If you prefer with jquery like (to remove an option with value 'TEST'):

$(countryField.Dropdown).find("option[value='TEST']").remove();
  • I like to disable them in some cases. $(countryField.Dropdown).find("option[value='TEST']").attr('disabled','disabled'); – AtariPixel Jan 27 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.