Enjoy an ad free experience by logging in. Not a member yet? Register.
|
|
Results 1 to 7 of 7
-
03-02-2016, 05:24 PM #1New to the CF scene
- Join Date
- Mar 2016
- Posts
- 7
- Thanks
- 5
- Thanked 0 Times in 0 Posts
object literals with input text fields values as key values
Hi!
I would like to generate object literals programmatically using the values of my text input fields but to say the truth I have no idea about how I can do that. I would like each object to be stored with the values of name, a sex and a form of address.
I would like to store each object in a big structure each time I create it like an empty arrayCode:
[/CODE]
// the
var OBJECTS = [];
$("input :text").each(function() {
// I added a loop here that almost crashed my computer. I do not know where to go from here.
}
[/CODE]
I have spent days on this any suggestions or links where I can find examples or documentation are more than welcome.
-
03-02-2016, 05:27 PM #2New to the CF scene
- Join Date
- Mar 2016
- Posts
- 7
- Thanks
- 5
- Thanked 0 Times in 0 Posts
so sorry I did not format it appropriately. Could someone please tell me how to modify the post? i cant see any option for that
-
03-02-2016, 05:54 PM #3New Coder
- Join Date
- Feb 2016
- Posts
- 80
- Thanks
- 0
- Thanked 16 Times in 16 Posts
You need to have minimum 10 posts to be able to edit a post. Klick "Go advanced" at the bottom, then you get buttons for formatting.
Unfortunately I do not completely understand what to do. Post the code of your inputs and an example what your object should look like.
You can create the fields of an object in the following way:
Code:var obj = []; var fieldname1 = "field1"; var value1 = "value1"; var fieldname2 = "field2"; var value2 = "value2"; obj[fieldname1] = value1; obj[fieldname2] = value2;
-
Users who have thanked Sempervivum for this post:
princesse (03-03-2016)
-
03-02-2016, 08:58 PM #4Master Coder
- Join Date
- Sep 2005
- Location
- Sydney, Australia
- Posts
- 8,055
- Thanks
- 2
- Thanked 808 Times in 797 Posts
You'd do better to specify var obj = {}; in that code as you are not able to use any of the array functionality on the properties you are adding.
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";which makes it easier to find errors in your code.
-
Users who have thanked felgall for this post:
princesse (03-03-2016)
-
03-03-2016, 09:04 AM #5New to the CF scene
- Join Date
- Mar 2016
- Posts
- 7
- Thanks
- 5
- Thanked 0 Times in 0 Posts
Good morning
I have something like this now with two text input fields
The problem is that the syntax is not correct I do not know how to tell it it should assign the current value of a specific text field with a specific id to the variable so that I can assign it to the key value of my object.
Code:$(':text').each(function(){ var firstName = $(this). attr('id Firstname').val(); var Address = $(this).attr('id address').val(); })
-
03-03-2016, 10:02 AM #6New Coder
- Join Date
- Feb 2016
- Posts
- 80
- Thanks
- 0
- Thanked 16 Times in 16 Posts
Is this what you need?
where "firstname" and "address" are the IDs of your textfields.Code:var obj = {}; obj.firstname = $("#firstname").val(); obj.address = $("#address").val();
-
Users who have thanked Sempervivum for this post:
princesse (03-03-2016)
-
03-03-2016, 02:17 PM #7New to the CF scene
- Join Date
- Mar 2016
- Posts
- 7
- Thanks
- 5
- Thanked 0 Times in 0 Posts
thank you very much.



Reply With Quote
