Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  • Thread Tools
  • Rate This Thread
  1. #1
    New 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.

    Code:
    
    
    I would like to store each object in a big structure each time I create it like an empty array

    [/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.

  2. #2
    New 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

  3. #3
    New 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;

  4. Users who have thanked Sempervivum for this post:

    princesse (03-03-2016)

  5. #4
    Master Coder felgall's Avatar
    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.

  6. Users who have thanked felgall for this post:

    princesse (03-03-2016)

  7. #5
    New 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();
    })

  8. #6
    New Coder
    Join Date
    Feb 2016
    Posts
    80
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Is this what you need?
    Code:
    var obj = {};
    obj.firstname = $("#firstname").val();
    obj.address = $("#address").val();
    where "firstname" and "address" are the IDs of your textfields.

  9. Users who have thanked Sempervivum for this post:

    princesse (03-03-2016)

  10. #7
    New to the CF scene
    Join Date
    Mar 2016
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts
    thank you very much.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •