0

Data Structure

var X = {
   a: [{name:"john", phone:777},{name:"john", phone:777},{name:"john", phone:777}],
   b: [{name:"john", phone:777},{name:"john", phone:777},{name:"john", phone:777}],
   c: [{name:"john", phone:777},{name:"john", phone:777},{name:"john", phone:777}],
   d: [{name:"john", phone:777},{name:"john", phone:777},{name:"john", phone:777}]
}

Function

function showTable(trnum,X,a) {
    var Tablecode = "<table><tbody>";
    for (var i=0; i< X.a.length;i++) {
        for (var j=0; j< trnum; j++) {
            Tablecode += "<tr><td>"+X.a[i].name+"</td><td>"+X.a[i].phone+"</td></tr>";
        }
    }
    Tablecode += "</tbody></table>";
    $("#elem").append(Tablecode);
}

Markup

<body>
    <button onclick="showTable(4,X,"a");"></button>
    <div id="elem">
    </div>
</body>

I'm trying to output data to table according to letter. one object on one table row.(all objects in table within one letter). this code doesn't work- unexpected token "}". oddly enough, it points to <div id="elem"> line. but there's no such character! How can I fix this?

UPDATE: thanks, error disappeared... but it outputs the whole structure, how do I make it output only 'a' objects?

3 Answers 3

1
<button onclick="showTable(4,X,"a");"></button>

should be like that (doublequotes):

<button onclick="showTable(4,X,'a');"></button>

Compare this one to yours (see .a and [a])

for (var i=0; i< X[a].length;i++){
 for (var j=0; j< trnum; j++){
 Tablecode += "<tr><td>"+X[a][i].name+"</td><td>"+X[a][i].phone+"</td></tr>";
 }

}

Feature reading about javascript notations

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

7 Comments

thanks, error disappeared... but it outputs the whole structure, how do i make it output only 'a' objects??
12 beacuse X[a]length(=3) * trnum (=4) = 12 You have 2 loops
i see.. is there a chance to fix this?? to get only 3 (under 'a')johns??
yes, remove for (var j=0; j< trnum; j++){ and } after Tablecode += ... . When you done this you can also change: showTable(4,X,'a'); to showTable(X,'a'); and function showTable(trnum,X,a) { for function showTable(X,a) {
thanks a lot!! you nailed it!! :) sorry but i have last question: how to disable button after first click?? to make it clickable but unexecutable after first click??
|
0

From tossing your code into a fiddle and getting it to run, I noticed that your button is a failure point. You have to surround the 'a' with SINGLE quotes.

http://jsfiddle.net/YaZp2/1/

EDIT: Also, edited the output code added the ability to actually see if it works.

Comments

0

Double quotes used to delimit the onclick and also the params within.

Try

<button onclick="showTable(4,X,'a');">

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.