0

I've got a Rails app in to which I want to submit a series of comma-separated IDs in a text field. For instance, let's say I was adding three elements to an order w/ respective IDs 12, 2, and 4, I would like the field to read:

12,2,4

Which is easy enough. The problem shows up when I've changed my mind and I want to replace an element in a certain position, i.e., the start, to an element with an ID of let's say 3. How would I manage to submit 3,2,4 and not 2,4,3? I'm pretty stuck. And if there's a method of automatically putting commas only between the IDs (not at the start or the end) in the field that'd be hugely useful.

Cheers!

Edit: I've written this in an attempt to solve this problem. It feels close but no cigar yet. The src mentioned below contains one of the three IDs mentioned above, and there is a changing number of the #elementXs to which this function will be attached.

$('#element1').change(function() {
  var src = $(this).val();
  var array = $('#order').val().split(",");
  array.splice(0, 1, src).join(",");
  $('#order').val(array);
}
6
  • Mileorsohigh, you will find it much easier to work with separate fields rather than one field containing a composite value. jQuery then makes changes/deletions/additions trivial. Of course, the server-side code needs to be compatible with this approach. Commented Dec 6, 2012 at 2:55
  • I want an array in one field because the number of elements can vary significantly. Commented Dec 6, 2012 at 2:59
  • Yes, of course. You would generate them dynamically, which is also trivial. Commented Dec 6, 2012 at 3:01
  • Oh, generate the fields dynamically then compile them to submit them? Commented Dec 6, 2012 at 3:02
  • I'm not sure about "compile" but I'm not a rails person. I was assuming client-side generation. Rails requires a different mindset. Commented Dec 6, 2012 at 3:06

1 Answer 1

4

Three steps. You're starting with a string containing comma-separated integers.

  1. Split the string on the commas into an array of ids.
  2. Splice the new id into the array replacing the old id. You want to pass 3 parameters to splice:
    • The index of the element you want to start removing from (if you want to remove the first element, its index would be 0).
    • How many elements you want to remove (here you just want to remove the first element, so 1).
    • The new element to add in (in your case, it's src).
  3. Join the string back together on a comma.
Sign up to request clarification or add additional context in comments.

3 Comments

I've written this. It's not working but it feels close. var array = $('#orders').val().split(","); array.splice(1, 0, src).join(","); $('#orders').val(array);
@mileorsohigh, I'm not sure you're using splice correctly. What is the value of src in your code? Edit your question to contain the code, it'll be easier to reason about there than in comments.
Perfect! Thank-you. I'll update my code for a working version should it help someone else.

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.