0

how can i get the ID or Name Field then append it to Textbox, Please Correct me if i'm approaching this in the wrong way.

$(document).ready(function(){
    var students = {!! json_encode($students->toArray()) !!};

    //This Part Im Trying to get.
    var name=$(students).val(name);
    var id=$(students).val(id);

    console.log(name);

});

Controller:

 $students=Student::all()->take(5);
 return view('Examples.levels',compact('students'));

image below is the result if i use console.log(students) enter image description here

Updated Version

$(document).ready(function(){
    var students = {!! json_encode($students->toArray()) !!};
    students.forEach(function(student) {
  $('.content').append(
  `<input name="name[]" value=`+student.name+`>`
  );
  console.log(student.name)
});
});

Attached imaged result: enter image description here

Copain
  • 163
  • 3
  • 18
  • There are some problems. first, your jquery selector, I mean, where you want to show your output, and second, there are 5 students data. which student data you want to show? – hasan05 Feb 03 '20 at 08:13
  • 1
    1. how to get the name only. 2.Append all 5 student in a input field – Copain Feb 03 '20 at 08:15
  • what is the input field's selector? I mean, the input field name / id ? – hasan05 Feb 03 '20 at 08:16
  • Something like this – Copain Feb 03 '20 at 08:19
  • Does this answer your question? [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – hasan05 Feb 03 '20 at 08:19
  • Do you want to show the names on different inputs? or only one input.? – hasan05 Feb 03 '20 at 08:58
  • it should out-put 5 input fields something like loop them into an input field then attached the value to its attribute, – Copain Feb 03 '20 at 09:00
  • i tried this but does't work i'm only gettng one field populated rest of the 4 is empty.(students.forEach(function(student) { $('.content').append(''); $("#myinput").val(student.name);) – Copain Feb 03 '20 at 09:05
  • @Copain Why don't you simply do this with blade ? – Steve Chamaillard Feb 03 '20 at 09:09
  • trying to play around with ajax – Copain Feb 03 '20 at 09:11
  • @Copain Your above code cannot work as you're using IDs which are unique in HTML. So jQuery selects only the first one even if you have 5 of them. Use classes instead. – Steve Chamaillard Feb 03 '20 at 09:11
  • @Steve Chamaillard hello can u tell me what are those? or can send link for me to read and learn on it.. – Copain Feb 03 '20 at 12:52
  • @Copain Looking for "HTML class" in DDG : https://www.w3schools.com/html/html_classes.asp is the first link. – Steve Chamaillard Feb 03 '20 at 14:34
  • @ Steve Chamaillard thanks.. i end up using AJAX in controller it self return it as json response then use ajax in view thats the best way so far..hehe thanks for ur advice..$.get method – Copain Feb 03 '20 at 14:47

2 Answers2

1

Try this one. it will append with value.

students.forEach(function(student) {
  $('.content').append(
  `<input name="name[]" value="`+student.name+`">`
  );
});
hasan05
  • 904
  • 5
  • 22
0

You should iterate over a students array like so:

students.forEach(function(student) {
   console.log(student.name);
   console.log(student.id);
});
Pavel Lint
  • 3,252
  • 1
  • 18
  • 18