JavaScript Access dynamically created objects in Javascript access dynamically created element jquery,access dynamically created controls c#,

I am attempting to dynamically create an object with name-value pairs using html input fields. This is what i have so far:

var taskComplete = {
  newTask: function(arg) {
    var fieldObj = {},
        selector = arg,
        inputFields = document.getElementsByClassName(selector);          
    for (var i=0;i<inputFields.length;i++) {
      var attrValue = inputFields[i].getAttribute("name"),
          value = inputFields[i].value, 
          newFieldObj = fieldObj[i] = {[attrValue]: value };
      console.log(newFieldObj);             
    }      
  }
}
taskComplete.newTask('input');

The console log outputs:

Object {title: ""}
Object {description: ""}
Object {date: ""}

Question1: How do i access these objects? they all share the same name eg. 'Object' so 'object.title' does not work.

Question2: Is there a way to combine these objects into a single object? eg.

var obj = {
  title: "",
  description: "",
  date: "",
}

code example with html: codepen.io

Hope this make sense. Thank you for any and all assistance.

Answer:1

I will answer your questions in the reverse order, makes more sense, you will see.

Question2: Is there a way to combine these objects into a single object?

Yes, I am happy to inform you that you absolutely can. Where you do

newFieldObj = fieldObj[i] = {[attrValue]: value };

simply do

fieldObj[attrValue] = value;

What this does, is the following: On the fieldObj, which is a plain {} set a key named after the value of attrValue and pair that key with the value value

Question1: How do i access these objects? they all share the same name eg. 'Object' so 'object.title' does not work.

In order to be able to access these objects, your newTask method should be returning them. So, at the end of your newTask method, simply do return fieldObj; like so

var taskComplete = {
  newTask: function(arg) {
    var fieldObj = {},
        selector = arg,
        inputFields = document.getElementsByClassName(selector),
        attrValue,
        value;

    for (var i=0;i<inputFields.length;i++) {
      attrValue = inputFields[i].getAttribute("name");
      value = inputFields[i].value;
      fieldObj[attrValue] = value;     
    }      
    return fieldObj;
  }
}

and then store the returned value to a new variable like so:

var aFancyName = taskComplete.newTask('input');

Here's the modified pen: http://codepen.io/anon/pen/XdjKQJ

Answer:2

I tried to check if the input entered by a user is a number or not with the code below. It didn't work. var a = Number(prompt("Enter a number")); if (a !== NaN) { console.log("yes"); } else { ...

I tried to check if the input entered by a user is a number or not with the code below. It didn't work. var a = Number(prompt("Enter a number")); if (a !== NaN) { console.log("yes"); } else { ...

  1. check input number javascript
  2. check input number jquery
  3. check input number java
  4. check input number
  5. check input number c#
  6. check input number python
  7. check input number html
  8. check input number is armstrong or not
  9. check input number is perfect or not
  10. check number input js
  11. javascript check input number only
  12. jquery check input number only
  13. check input if number javascript
  14. check if input number is empty
  15. check if input number python
  16. php check input number
  17. bootstrap check input number
  18. check input is number or not in javascript
  19. bash check input number
  20. vuejs check input number

How to replace sequential ampersand symbol leaving single ampersand untouched. Below is the script I tried which replaces every ampersand into double semicolon. <html> <body> <p id="...

How to replace sequential ampersand symbol leaving single ampersand untouched. Below is the script I tried which replaces every ampersand into double semicolon. <html> <body> <p id="...

  1. javascript regex replace all
  2. javascript regex replace group
  3. javascript regex replace $1
  4. javascript regex replace with capture group
  5. javascript regex replace special characters
  6. javascript regex replace string
  7. javascript regex replace example
  8. javascript regex replace tester
  9. javascript regex replace match
  10. javascript regex replace non alphanumeric
  11. javascript regex replace spaces
  12. javascript regex replace all spaces
  13. javascript regex replace all matches
  14. javascript regex replace backreference
  15. javascript regex replace characters
  16. javascript regex replace not working
  17. javascript regex replace callback
  18. javascript regex replace variable
  19. javascript regex replace non numeric
  20. javascript regex replace forward slash

I am trying to convert a csv file to json using csvtojson converter in nodejs.My code is as shown below. I get an output of data as shown below. I am not sure why this is happening and how to prevent ...

I am trying to convert a csv file to json using csvtojson converter in nodejs.My code is as shown below. I get an output of data as shown below. I am not sure why this is happening and how to prevent ...

  1. csv to json conversion using python
  2. csv to json conversion using java
  3. csv to json conversion in java
  4. csv to json conversion in python
  5. csv to json conversion in javascript
  6. csv to json conversion in mule
  7. convert csv to json using python
  8. convert csv to json using c# and asp net
  9. convert csv to json using javascript
  10. convert csv to json using java
  11. convert csv to json using jackson
  12. convert csv to json using pandas
  13. convert csv to json using c#
  14. convert csv to json using node js
  15. convert csv to json using excel
  16. convert csv to json using php
  17. convert csv to json using jq
  18. convert csv to json using bash
  19. convert csv to json using pyspark
  20. convert csv to json using powershell

I am new to code-igniter. In my View I added this code to enable google login in my website. <html lang="en"> <head> <meta name="google-signin-scope" content="profile email"> <...

I am new to code-igniter. In my View I added this code to enable google login in my website. <html lang="en"> <head> <meta name="google-signin-scope" content="profile email"> <...

  1. send data from javascript to python
  2. send data from arduino to raspberry pi
  3. send data from child to parent react
  4. send data from flask to html
  5. send data from activity to fragment
  6. send data from android to iphone
  7. send data from javascript to python flask
  8. send data from react to node
  9. send data from python to html
  10. send data from one arduino to another
  11. send data from s3 to kinesis
  12. send data from arduino to processing
  13. send data from matlab to arduino
  14. send data from server to client
  15. send data from raspberry pi to pc
  16. send data from child to parent angular
  17. send data from python to arduino
  18. send data from iframe to parent
  19. send data from javascript to php
  20. send data from react to express