2

I am appending the .text() of an anchor to an input when its dragged and dropped to it.

The thing is that I need them comma sepated and not repeated:

If I am tying to add 'string 1' to the input when the value is already 'bla, ble, string1', I need to prevent to be duplicated,

How would you do it?

My first guess is to make an explode by ',' and combine it with a for loop, but I don't think that is really optimized at all.

3
  • Why not use a regex to match string1 between two commas? Of course you'd need to account for it being first or last element too when building your regex. Commented Jan 27, 2012 at 14:03
  • 1
    BTW, is there a good reason why you need to store a big string? Why don't you keep the easier to manipulate "exploded" array instead? Commented Jan 27, 2012 at 14:13
  • stackoverflow.com/questions/890782/javascript-function-inarray Commented Jan 27, 2012 at 14:15

4 Answers 4

2

Basic Idea

function addParameter(str, param) {
    var re = new RegExp("[^,]\\s?" + param + "[,$]","g");
    if( !re.test(str) ) {
        str += ( (str.length>0) ? ", " : "") + param;
    }
    return str;
}

var orgStr = "abc, def, hij";

//This exists so it will not be added
var newStr = addParameter( orgStr, "def" );
console.log( newStr );

//This does not exist so it will be added to the end
newStr = addParameter( orgStr, "xyz" );
console.log( newStr );

Explain Regular Expression

  • [^,]\\s? - Says match beginning of string or a comma followed by an optional space character
  • param - matches your string passed in
  • [,$] - Says match a comma or the end of a string.
Sign up to request clarification or add additional context in comments.

Comments

0

Convert your existing CSV string to an array, append your value, and then convert it back to a string:

//Create an array from the CSV list
var myVals = $("#YourElement").val().split(",");
var isDupe = false;

for(var i = 0; i < myVals.length; i++) {
    if(myVals[i].indexOf("YourNewValue") != -1) {
        isDupe = true;
        break;
    }
}

//use .push() to append your value to the end of the array
if(!isDupe) {
    myVals.push("YourNewValue");

    //Invoking .toString() on an array creates a CSV list
    $("#YourElement").val(myVals.toString());
}

Here's a working fiddle.

1 Comment

@ToniMichelCaubet, Please see updated answer for dupe checking.
0

Based on your fiddle you can;

var myVals = $("#YourElement").val().split(",");
var wantToAdd = "cakey";

if ($.inArray(wantToAdd, myVals) === -1)
   myVals.push(wantToAdd);

$("#YourElement").val(myVals.join(","));

Comments

0

I don't see the need for reg exp or array-

var t=input.value, s= //text string;
if(t.indexOf(s)==-1)t.value=t+ ', '+s;

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.