31336

Jquery Each Json Values Issue

Question:

<html> <head> <title>testjson</title> <script type="text/javascript" src="jquery-1.7.2.min.js"></script> <script type="text/javascript"> var incidentReport1 = { "text1": "n/a", "text2": "n/a", "text3": "n/a", } function readHtmlForInputs() { var count = 0; //Setting count to 0 $('input').each(function(){ var input = $(this); var temp = (input.attr('id')); if(input.attr('type') == 'button'){alert('button');} else{ incidentReport1.temp = input.val(); count++; //Incrementing counter } }); console.log("Input Types Found:" + count); } function saveChanges() { readHtmlForInputs(); console.dir(incidentReport1); } </script> </head> <body> <input type="button" value="save" onclick="saveChanges();"/> <input type="text" name="Surname" id="text1" class="InputText" /><br> <input type="text" name="Surname" id="text2" class="InputText"/><br> <input type="text" name="Surname" id="text3" class="InputText"/><br> </body> </html>

Got the above block of code, an i want to be able to dynamic take the inputs ID, and use the ID to assign a value in incdientReport1 json. When i do this issue seems to be this line...

var temp = (input.attr('id'));

When assigning the ID and displaying it in console it works fine

But when this line of code

incidentReport1.temp = input.val();

Runs it saves it to a new feild called temp rather than the string value of temp..

So confused guys where am i going wrong?

Answer1:

You want:

incidentReport1[temp] = input.val();

When you need to use a computed property name, you use the [ ] operator. That is,

object.propertyName

is the same as

object[ "propertyName" ]

Recommend

  • Scroll multiple TextViews simultaneously
  • Regex - Match everything except HTML tags
  • Can I click to get to a method/object in Sublime Text like I can in NetBeans
  • PreparedStatement - how specify to use default value of column
  • jquery change text on div
  • Bind two JSF input fields with single property of backing bean
  • Replace HTML links with text
  • jquery code not working without breakpoint
  • NSIS decompiler
  • How to 'create temp table as select' in Slick?
  • How load cv2.KeyPoint and Descriptors correctly on OpenCV 3 with Python 3?
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • Using Sax parsing to edit and write XML in VB6
  • saving file generated by TCPDF
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Passing 1 of 3 values to a Sub
  • d3 v4 drag and drop with TypeScript
  • Django: Count of Group Elements
  • Paperclip, set path outside of rails root folder
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Getting last autonumber in access
  • Counter field in MS Access, how to generate?
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • MySQL WHERE-condition in procedure ignored
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • what is the difference between the asp.net mvc application and asp.net web application
  • jquery mobile loadPage not working
  • Matrix multiplication with MKL
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • How to stop GridView from loading again when I press back button?
  • Linking SubReports Without LinkChild/LinkMaster
  • Binding checkboxes to object values in AngularJs
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?