0

I am writing a jQuery function on the click event of a checkbox list, which stores the value in a string when one item is checked and it removes the string when it is unchecked. I am taking the closed label text and insert and remove it from the string. I can add the strings but I am not able to remove it.

Here is my code:

var currentage = '';

$('#<%=chk_ange.ClientID%> input:checkbox').click(function () {
    var str = $(this).next('label').text();
    if ($(this).is(':checked')) {
        if (currentage.indexOf($(this).next('label').text()) == -1) {
            currentage = currentage + str;
        }
        alert('checked' + currentage);
    }
    else {
        currentage.replace(str, "Hello");
        //currentage.replace(str, 'None');
        alert('unchecked' + currentage);
    } 
}

I am storing the values in a global variable so that i can compare the value in each click.

juan.facorro
  • 9,791
  • 2
  • 33
  • 41
user2664298
  • 175
  • 1
  • 7
  • 22

2 Answers2

3

You have to assign the result of replace back to your variable like:

currentage = currentage.replace(str, "Hello");

See: String.prototype.replace() - MDN

Habib
  • 219,104
  • 29
  • 407
  • 436
0

You need to equal the result from currentage.replace(str, "Hello"); to currentage. String are immutable so the replace()function returns a new modified string.

currentage = currentage.replace(str, "Hello");
juan.facorro
  • 9,791
  • 2
  • 33
  • 41