views:

39

answers:

4

I'm new to JQuery so this question might be an obvious one, but I have something that appends a text to what's already in an input box:

$('a.blog_category').click(function(){

  var current_value = $('#id_category').val();

  $('#id_category').val(current_value + ', '+ this.text);
  return false
})

I'd like to add an if clause that sounds like:

"If there is already a comma at the end of the line, don't add a comma." "If there is not already a comma AND it's not the first item in the input text, add a comma."

I hope that makes sense.

Thanks for any help beforehand.

+1  A: 

not so sure if jQuery has a helper function for it, but you can achieve this using plain Javascript with the following:

if (current_value.charAt(current_value.length - 1) != ',') {
    current_value = current_value + ',';
}
Richard Neil Ilagan
or rather, current_value += ',';
Richard Neil Ilagan
This with a little modification for clause #2 gives me what I wanted: if (current_value.length != 0 } Thank You! P.S: How do I add a line break in the comment?
MonkeyBoo
@MonkeyBoo ~ re line breaks, I seriously don't know. Been trying to figure that out myself. :D
Richard Neil Ilagan
+1  A: 

Here's an updated function with how I would accomplish this using regex's.

$('a.blog_category').click(function(){

     var current_value = $('#id_category').val();
     if (!current_value.match(/,$/) && !current_value.match(/^,/)) {
         // no commas were found in the wrong places :)
         $('#id_category').val(current_value + ', '+ this.text);
         return false;
     } else {
         // commas were found...don't put a comma :(
         $("#id_category").val(current_value + ' ' + this.text)
});
Alex
I'm not sure why you're using regex, or why a character class, instead of just `/,$/`. I think "it's not the first item in the input text" means, there's already something in the box. I don't see any discussion of special handling for a comma at the beginning.
Matthew Flaschen
Simply personal preference. I like using character classes wherever possible. However, I have removed them from this answer. And I was just trying to match his specifications the closest I could. It's redundant I suppose. But that's why.
Alex
+1  A: 

The simplest way is to just write the logic to check for all that you mentioned. There might be a cleaner way with selectors but I would have to spend more time thinking about that. But doing something like this should work:

$('a.blog_category').click(function(){

  var current_value = $('#id_category').val();

  if (current_value.charAt(current_value.length - 1) != "," && current_value.indexOf(",") > -1)
{
  $('#id_category').val(current_value + ', '+ this.text);
}
else
{
  $('#id_category').val(current_value + this.text);
}
  return false
})

EDIT: Skip above. I think you are just looking for something like this so maybe this will work better. No logic really needed:

$('a.blog_category').click(function(){

  var current_value = $('#id_category').val();
    var parts = current_value.split(",");

    parts.push(this.text);

if (parts[0] == "")
    parts.splice(0,1);

  $('#id_category').val(parts.join(","));

  return false
})​
spinon
This will always require that there's already a comma (`> -1`), so you can't add the first one.
Matthew Flaschen
@Matt Yeah I realized that. But that is what it sounded like was explained in the question. But regardless I just reworked it to be cleaner.
spinon
@spinon, yeah, the question could definitely use some clarification.
Matthew Flaschen
A: 

Try:

$('a.blog_category').click(function(){

  var current_value = $('#id_category').val();

stringArray = current_value.split(",");  if(stringArray.length>= 1) {

//Split the string into an array and check the number of items in array

if (current_value.charAt(current_value.length)!=","){

//Check what the last character in the string is - apply comma if needed

  $('#id_category').val(current_value
+ ', '+ this.text);

} }   return false })
dpmguise