How to pass a fieldset to javascript?

When my user clicks a button, I want to access the fieldset by javascript. Can i do that?

  <apex:commandbutton value="Send"  onClick="check('{$ObjectType.Customization__c.FieldSets.Fields_for_fieldChooser}')" />


    function check(apiNameList) {

        for (var i = 0; i < apiNameList.length; i++) {
            var apiName = apiNameList[i];

            var updateVal = apiName + "_update";
            var deleteVal = apiName + "_delete";
            var dontaddVal = apiName + "_do_not_add";
            //    if ( document.getElementById(updateVal).checked === false && document.getElementById(deleteVal).checked === false && document.getElementById(dontaddVal).checked === false){
            alert("Please choose an option for each field");

            //     }



Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

To get quotes round the field names so you have a JavaScript array of strings, you need to turn the Apex array into a JavaScript array by for example using an apex:repeat:

var fields = [
<apex:repeat var="f" value="{!$ObjectType.Customization__c.FieldSets.Fields_for_fieldChooser}">

The above Visualforce produces this:

var fields = [
   ' Field3__c',

Then adapt you other JavaScript to reference this array. (Note that the spurious trailing comma is allowed in JavaScript).

Or if you need the JavaScript arrays to be inline, this will work:

... onClick="check([<apex:repeat var="f" value="{!$ObjectType.TestRelationship__c.FieldSets.TestFieldSet}">'{!f}', </apex:repeat>])" ...

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x