0

I am using a onclick function and adding a value to by paasing a value to it. If the value already exists den i remove that value from the list.

function send_value(str)
    {
    //alert(str);
    var sub_id = document.getElementById('selected_sub_id').value;
    //alert(sub_id.indexOf(str));
    if(sub_id.indexOf(str)==-1)
    {
        if(sub_id=="")
        {
            sub_id+=str;
        }
        else
        {
        sub_id+=',';
        sub_id+=str;
        }
    }
    else
    {
        sub_id = sub_id.replace(str+",", "");
        sub_id = sub_id.replace(","+str, "");
        sub_id = sub_id.replace(str, "");
    }
    //alert(sub_id);
    document.getElementById('selected_sub_id').value=sub_id;
}

This is the function. Suppose i have the values 1,2,3,4 in the selected_sub_id and i am passing 5 to it, it will be stored as 1,2,3,4,5 now i am passing 24 it will be stored as 1,2,3,4,5,24 No suppose i want to remove 2, so when i send 2 to the function it removes all the occurrences of 2 so i am left with only 1,3,4,54... kindly help me with this thanks in advance..

1 Answer 1

2

You most probably want an array for this. It makes things much easier.

Create one first:

var arr = [];

Adding goes with .push:

arr.push(24);

Removing goes with .splice and .indexOf (the 1 means 'removing 1 element'):

arr.splice(arr.indexOf(24), 1);

Converting to a string goes with .join:

arr.join(); // 1,2,3,24 or something similar depending on elements

For example: http://jsfiddle.net/ScBNQ/.

var arr = [];

// adding

arr.push(1);
arr.push(2);
arr.push(3);
arr.push(24);

// removing

arr.splice(arr.indexOf(2), 1);

// joining

arr.join(); // 1,3,24
Sign up to request clarification or add additional context in comments.

5 Comments

How do i kno that the element I am send is in the array or not
@lucky123: arr.indexOf(24) will return -1 if 24 is not in the array, otherwise it will return the index it is at.
var arr = []; var arr = document.getElementById('selected_sub_id').value; //alert(sub_id.indexOf(str)); if(arr.indexOf(str)==-1) { arr.push(str); } else { arr.splice(arr.indexOf(str), str); arr.join(); } //alert(sub_id); document.getElementById('selected_sub_id').value=arr; Iam doing it this way !!!! tell me if its wrong. it says arr.push(str); is not a function;
@lucky123: I should have been a little clearer. Have a look at this fiddle: jsfiddle.net/ScBNQ/1. You can combine add/remove into one button with what I said in my first comment.
@lucky123 from your code in your comment above, you are getting an error because your are assigning arr as an array and then immediately re-assigning it to document.getElementById('selected_sub_id'); so of course arr doesn't have the push function.

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.