2

the selected value is: ["4", "5", "6"]

The code which I have now:

$.each(selected, function(){
    $('input[type=hidden][name="ids[]"]').val(selected);
});

is resulting in:

array:1 [▼
  0 => "4,5,6"
]

I need these selected elements to be their own index of the array so that I can loop through it in my controller. Could someone please help me assign the seperate array values to a hidden value

2
  • array[0].split(',') Commented Jul 31, 2017 at 14:37
  • can you please tell me where?? @Laazo Commented Jul 31, 2017 at 14:40

2 Answers 2

1
  1. Create new array using .each() function to loop on all values.
  2. It accepts 2 parameter index and value

 var selected = ["4", "5", "6"]
var arr = []
var obj = {}
$.each(selected, function(i,v){
   
   obj[v]=v
   
});
arr.push(obj)
console.log(arr)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

1

Create a new Array and push the Results into it.

var values = [];
$('input[type=hidden][name="ids[]"]').each(function ( index, element ) {
    values.push( ... );
});

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.