Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.
var fromFields = [<apex:repeat value="{!fromFields}" var ="f">'{!f}',</apex:repeat>];

renders as

fromFields =['abc','msr','mdr',];

Now I want to remove comma after 'mdr' so it will change to

fromFields =['abc','msr','mdr'];

How we can achieve this using jQuery or JavaScript?

share|improve this question

closed as off-topic by Jonathan Hersh, greenstork, James Loghry, Phil Hawthorn, Mark Pond May 29 '14 at 21:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions not specific to Salesforce are off-topic here, but can be asked on Stack Overflow." – Jonathan Hersh, greenstork, James Loghry, Phil Hawthorn, Mark Pond
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Are you talking about some JavaScript generated by Visualforce? If you are you should probably include that Visualforce; otherwise this question will get closed as not being related to Salesforce. –  Keith C May 29 '14 at 18:47
    
var fromFields = [<apex:repeat value="{!fromFields}" var ="f">'{!f}',</apex:repeat>]; fromFields =['abc','msr','mdr',]; Now I want to remove comma after 'mdr' and I want to change fromFields =['abc','msr','mdr']; How we can achieve using Jquery or JavaScript. –  Bujji May 29 '14 at 18:57
    
@KeithC looks like Markdown formatting engine ate the tags ;) –  eyescream May 29 '14 at 19:04
    
Bujji, why exactly you need to do it? In Chrome and Firefox this becomes a proper array (4th element disappears), I've even checked fromFields.length property, it returns "3". –  eyescream May 29 '14 at 19:07
    
I would not recommend building your array in this way. Try using javascript remoting instead or if that is not possible serialize your array on the server and deserialize it with javascript. –  Phil Rymek May 29 '14 at 19:07

2 Answers 2

up vote 6 down vote accepted

While the extra comma shouldn't harm anything (JS does not care in all modern browsers), you'd better off encoding this in JSON beforehand:

// Controller Code

public String getFromFields() {
    return JSON.serialize(fromFields);
}

// JS Code

var myarray = {!fromFields};

Here's a fully functional example:

Controller

public with sharing class arrayjson {
    string[] fromFields = new List<String>{'Hello','Master','Fear'};

    public String getFromFields() {
        return JSON.serialize(fromFields);
    }
}

Page

<apex:page controller="arrayjson">
<script>
var fromFields = {!fromFields};
for(var field in fromFields) {
    alert(fromFields[field]);
}
</script>
</apex:page>

Using apex:repeat

If you absolutely want to build the array using repeat:

var fromFields = [];
<apex:repeat value="{!fromFields}" var="field">
fromFields.push("{!field}");
</apex:repeat>
share|improve this answer
    
But now he gets a string he has to call split() on ;) –  eyescream May 29 '14 at 19:10
    
Nope. It can be done literally. I've done this personally. –  sfdcfox May 29 '14 at 19:11
    
O, nice. Every day is a school day :) –  eyescream May 29 '14 at 19:12
    
Is their any other way because i dont want increase code in controller –  Bujji May 29 '14 at 19:23
    
@Bujji You can use the repeat tag; use the example I just added in this answer. –  sfdcfox May 29 '14 at 19:48

Assuming you are having issue with the last value coming up as being 'undefined' (IE7 handling of arrays with trailing ','), you should be able to use jQuery like this

$.each(arr,function(index, value){if (value === undefined){arr.splice(index,1);};});
share|improve this answer
    
do we need pass values for index an value or not? –  Bujji May 29 '14 at 19:35
    
the values for index, and value will be populated by the $.each(). As long as the name of your array is "arr" it can be used exactly as I gave above. If you named your array something different, be sure to replace both "arr"s with the actual name of your array –  Tezyn May 29 '14 at 19:55

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