1

i want to create a web page using javascript and html, which takes details of many students from the user and dynamically store it into an array. so i guess what i need is an array of arrays which can be dynamically given values to. i created a page to get details of students like "first name". "last name", "admission number", and "class". i created a form and got the values like this.

form = document.std_form;
f_name=form.firstname.value ; 
l_name=form.lastname.value ;
a_no=form.ad_number.value ;
c_no=form.class_no.value ; 

f_name holds firstname and so on.. now i want to create an array student_list which holds arrays std1, std2, std3 etc each which holds the details of separate students. pls tell me how to create such an array and also how i can display each elements.

Mathew
  • 11
  • 3

2 Answers2

1

You can have an array name student_list which holds all students and an array student which holds individual students.

var student_list = new Array(); // this will have to be initialized only once

var student = new Array();  // create instances for students with every student being entered through form

// code to populate student array

student_list.push(student); // push students one by one in main list. Should be executed with each student being saved.

Please modify code according to your needs.

You can have a look at this to see how to iterate through it.

Hope it helps !!

Community
  • 1
  • 1
Cyclone
  • 1,580
  • 1
  • 8
  • 14
  • thanks a lot. can u please tell me how to create the array student, containing fields "first name", "last name", "admission number" and "class"? i just want to declare the array sothat i can dynamically assign values to it when the user enters it. – Mathew Oct 10 '12 at 12:53
  • Every time when we click on some button like "Save" or something, execute this code: `var student = new Array();` then `form = document.std_form;` `student.push(form.firstname.value);` and respective fields. In the end do this `student_list.push(student);` – Cyclone Oct 10 '12 at 13:00
  • So, whatever you were doing in your code, just create an array on the top, and push the form values in that array, and push that array in student_list. Then you got it. – Cyclone Oct 10 '12 at 13:06
0

Here's an example.

[EDIT]

var studentList = new Array();
var curStudent = new Object();

    var i, num;

    num = getNumberOfRecordsToAdd();    // how ever it is that you know how many the user wants to add

    for (i=0; i<n; i++)
    {
        curStudent.f_name = getFname(i);    // however it is you retrieve these from where ever they are at the moment
        curStudent.l_name = getLname(i);
        curStudent.a_no = getAno(i);
        curStudent.c_no = getCno(i);
        studentList.push(curStudent);
    }

This code will give you an array of items. Each item is an object with 4 fields.

I.e:

//Student1:

    studentList[0]      // student 1 (student0)
    studentList[0].f_name   // student1's f_name
    studentList[0].l_name   // student1's l_name


//Student2:

    studentList[1]      //  student2
    studentList[1].a_no //  student2's a_no
    studentList[1].c_no //  student2's c_no
enhzflep
  • 12,927
  • 2
  • 32
  • 51
  • thanks, but i'm having a problem with this.. actually what i want is, i want curStudent[1] to store f_name, l_name etc of student1, curStudent[2] to store f_name, l_name etc of student2, etc like wise.. can u pls help me out?? – Mathew Oct 10 '12 at 13:05
  • You want a _multi-dimensional_ array, right? curStudent is used to hold the details of each student before it is added to the list of students. Do you wish to access the individual fields of info of each student via an array, or by name? – enhzflep Oct 10 '12 at 13:29
  • yea.. i want to access individual fields of student via an array, somewhat like student[i].curStudent.f_name.value ?? I dont kno wtr the syntax is right. – Mathew Oct 11 '12 at 05:45
  • Then in that case, you DONT want a multi-dimensional array. You just want a simply array of objects. I'll edit my answer in a few minutes. – enhzflep Oct 11 '12 at 05:54
  • actually we cant say howmuch current students would be there.. its decided by the user.. so we cant give f_name1, f_name2 etc.. that is the problem – Mathew Oct 11 '12 at 07:18
  • Yeah, I realize that - that's why I left the text 'unrolled example' in there. I'll update the answer again. – enhzflep Oct 11 '12 at 07:22
  • i tried that way, but in the array part, something is going wrong. my code is_ var i=0; var student_list = new array(); function submit() { var curr_student = new Object(); form = document.std_form; f_name=form.firstname.value ; l_name=form.lastname.value ; curr_student.f_n=f_name; curr_student.l_n=l_name; i++; student_list.push(curr_student); } – Mathew Oct 12 '12 at 11:21
  • then i tried to access an element by function show_form() { var q; student_list[2].f_n.value=q; alert(q); } it wont work.. – Mathew Oct 12 '12 at 11:29
  • No, it wouldn't work. First, q never holds anything, next you would need to say: var q = studentList[2].f_n; alert(q); - (lose the .value, .f_n *is* the value) you should get into the habbit of using the JavaScript debugger in your browser of choice. I prefer chrome, though any current browser will suffice. Hope this helps.. :-) – enhzflep Oct 12 '12 at 11:48
  • i'm using chrome too.. can u suggest a debugger?? – Mathew Oct 12 '12 at 11:53
  • Sure, just use the one in chrome.. (Ctrl-Shift I) or you can get to it via what used to be the 'wrench-menu' - which now looks like 3 horizontal lines if your browser is up to date. – enhzflep Oct 12 '12 at 12:05
  • thanks :) i tried with ur code. but, var q = student_list[2].f_n; wont work :( whenevr i type this code, the script wont work. – Mathew Oct 12 '12 at 12:42
  • I've gotta hand it to you for tenacity. Here, give this fiddle a try. I often learn better by looking at code, perhaps you do too? Here: http://jsfiddle.net/enhzflep/MTQ2q/ – enhzflep Oct 12 '12 at 13:16