0

I have a webpage where a user can select multiple items from a jquery list.

Based on the item(s) selected I need to add each item into the database.

When someone selects one item the value returned to my Javascript is similar to "4~2"

The value 4 would be used in my example for one column named "skill_id" in the database and the value 2 would be used for another column called "category_id" in the same row.

When someone selects two items it is comma-delimited and similar to "4~2,6~7" and so on if they select more than 2.

I'm thinking I need to do a for loop with an array or a jquery.each() function but not certain how the best way to approach this is.

1
  • 3
    Adding some code will gain the success of the answers ;) Commented Apr 27, 2012 at 22:39

2 Answers 2

16
+500

What you're looking for is the split() method.

"4~2,6~7".split(',') // ['4~2', '6~7']
Sign up to request clarification or add additional context in comments.

1 Comment

@Somebodyisintrouble yeah, this got me a "nice answer" badge ;o). Why did you start the bounty by the way?
2

Here is one way you could extract the skill_id and category_id:

  $.each("4~2,6~7".split(','), function(index, value) {
      var nums = value.split("~");
      var skill_id = nums[0];
      var category_id = nums[1];
  });

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.