I have a html form and 've many text boxes, dropdowns, and radios. I have given ids for each. Now i wanna populate the data from that using a js function.
Code:
function user(id,firstname,lastname){
this.id=id;
this.firstname=firstname;
this.lastname=lastname;
}
//hope i can include othr things when i need in future.
This way i wanna go. And onload i creates a function,
Code:
function getdatas()
now i want to get the datas entered in the form like this. After entering all the data in form when i clk a buttn i wanna populate those values using tis function. I just know that its an object of objects. Can ny1 explain n guide me How to do it? I m just a learner n please make it simple replys.
Can you translate that in English, please? As for me, I have not understood too much of your phrases.
Anyway, you should use that function as a constructor, by using the token new whenever use it:
Code:
function user(id,firstname,lastname){
this.id=id;
this.firstname=firstname;
this.lastname=lastname;
}
var John=new user(123,'John','Doe');
var Mary=new user(124,'Mary','Smith');
Can you translate that in English, please? As for me, I have not understood too much of your phrases.
Anyway, you should use that function as a constructor, by using the token new whenever use it:
Code:
function user(id,firstname,lastname){
this.id=id;
this.firstname=firstname;
this.lastname=lastname;
}
var John=new user(123,'John','Doe');
var Mary=new user(124,'Mary','Smith');
Sorry for my poor english usage. K.. I jus did it.
function populatedata()
{
var UserDetails = new User( document.getElementById("CenterID"), document.getElementById('something'));
alert(UserDetails.id ;
}
This is what i meant bro. This is working now. Im getting alert boxes aftr sumbitting data. So this is fine right? You told some constructor problem. Wats it? Is ther any error in this code.?
Now my other doubt is, I want to include 2 more fields with the function user. ie I want to add "address", " somotherthing" in the function 'user()'. How to add it? ( Without editing the user(id, center, firstName, lastName, mobileNumber) part. Can i add it like this.somthing..or nysuch ways are available??
Now my other doubt is, I want to include 2 more fields with the function user. ie I want to add "address", " somotherthing" in the function 'user()'. How to add it?
In fact you don't need to add it. Once you have created the Object, you may add custom properties, at will:
Code:
var UserDetails = new User( document.getElementById("CenterID"), document.getElementById('something'));
userDetails.address='5, Mark Avenue';
You may do the same thing in a more sophisticated way: using the prototype of the object:
I find that interesting, Kor, but I don't understand it. I don't mean to hijack the thread, but the point you illustrated intrigues me.
Why is it that this:
Code:
function User() {
/* ... properties */
}
User.prototype.addProperty = function(prop,value) {
this[prop] = value;
}
var you = new User();
you.addProperty('isnot','me');
is 'better' than this:
Code:
var you = new User();
you.isnot = 'me';
I guess I can see a 'best-practices' argument for it, kind of, but is it more or less efficient?
(Sorry, I may have edited this a few times while you were responding).
I did not say it is better. I said just that is another, more sophisticated way Maybe I wanted to make the OP curious what is about that mysterious prototype property/method
I did not say it is better. I said just that is another, more sophisticated way Maybe I wanted to make the OP curious what is about that mysterious prototype property/method
Thanks bro.. I got what i was looking for from your info.. thanks alot..
Bookmarks