0

Ok, i have been searching for 2-3 weeks now so I apologize if this has already been answered AND I apologize if I don't get the terminology exactly right. I'm a newbie.

I am creating a budgeting program to help me learn coding. I have 3 columns in an HTML table, and an obj with key/value pairs.

I would like to use the keys in the obj to create classes for my inputs so I can send them all to a database to track/add/do whatever...

var obj = { item1=10, item2=20, item3=30}
var getkey = Object.keys(obj) 

Here is one of the many loops I have tried.

for (var i = 0; i < getkey.length, i++) {
var key = getkey[i]
var keyclass = key + "id"
var appendage = "<input id='" + key + "id'" + "name='" + key + "spendinput' type='text' />"
if (i == 0) {
$("#firstrow td:nth-child(3)").append(appendage).addClass(keyclass).removeClass("spendinput")
} else if ($(".spendinput").hasClass(prevkeyclass) == false) {
$(".spendinput").append(appendage).addclass(keyclass)
}

/*Here is my HTML*/
<form method="POST" action="expense.php">
  <table>
   <thead>Current Budget</thead>
   <tbody>
    <tr id="firstrow">
     <td id="utilities"></td>
     <td id="utilitiestotspend" class="spendinput"></td>
    </tr>
    <tr>
     <td id="gas"></td>
     <td id="gastotspend" class = "spendinput"></td>
    </tr>
  </tbody>
 </table>
</form>

Thank you for any suggestions.

1
  • Your object is not setted corectly var obj = { item1=10, item2=20, item3=30}. You sould set the pair key value with : like var obj = { item1:10, item2:20, item3:30} Commented Oct 14, 2013 at 21:05

1 Answer 1

1

Your object has wrong syntax. To assign values you use :, not =.

var obj = { item1:10, item2:20, item3:30}

One more thing that could be helpful for your future learning is Firebug for Firefox. If you're using Chrome instead, F12 will do the same. These are very powerful tools that let you inspect/modify (on the fly) HTML, CSS, scripts, network traffic, debug javascript console and much much more. When you get used to it you can't live without it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.