0

Below is my code. First section is JS code and second is HTML.

$('#table').on('check.bs.table', function (e, row) {
    checkedRows.push({First: row.fname, Second: row.sname});
    var test = console.log(checkedRows);
    document.getElementById("xyz").innerHTML=test;
});


<p id="xyz"></p>

I want to basically output first and last name from table to my html page but somehow it's not working. The console.log prints just fine when selecting rows from table. Again I want to access the selected data from console.log to view on my html page.

3
  • I mean, sure, but, i don't think it'd be that useful to store undefined in a variable. (console.log doesn't return anything) Commented Feb 5, 2018 at 20:19
  • console.log(checkedRows) returns undefined, so you need to assign the value before you print it. Commented Feb 5, 2018 at 20:19
  • Just remove the console.log. Commented Feb 5, 2018 at 20:26

1 Answer 1

1

Use JSON Stringify if you just want to test your Array with Objects:

// Say, after several .push() our array looks like:

var checkedRows = [
  {First:"John", Second: "Doe"},
  {First:"Mary", Second: "Jane"},
];

// console.log(checkedRows);

document.getElementById("xyz").innerHTML = JSON.stringify(checkedRows, null, 4);
<pre id="xyz"></pre>

If you want to print only a string like "John Doe" or "Mary Jane" than use

$('#table').on('check.bs.table', function (e, row) {

    var person = {First: row.fname, Second: row.sname}; // Store data into variable

    checkedRows.push( person ); // Push person Object to checkedRows Array

    console.log( checkedRows );
    console.log( person );

    document.getElementById("xyz").innerHTML = person.First +" "+ person.Second ;
    // or use:
    document.getElementById("xyz").innerHTML = row.fname +" "+ row.sname ;
});
Sign up to request clarification or add additional context in comments.

5 Comments

document.getElementById("xyz").innerHTML can be shortened to xyz.innerHTML.
^ true...I also think the syntax he used is fine, I can see people looking around for your xyz variable not understanding it's an element.
@connexo yes, but we should stop learning from w3schools finally ;) - You should not break your code if you introduce suddenly an xyz variable. Also, can you say with 100% certainty it's not a name (instead of id)? No. :)
@Roko C. Buljan it worked. Although the output is like [ {First:"John", Second: "Doe"}, {First:"Mary", Second: "Jane"}, ] anyway I can just get the values. For example I just want to print "John" "Doe" when selecting the row and not the [ {First:"John", Second: "Doe"}, ] ?
@Hari notice that I used the <pre> tag to get a nicely formatted output. And see my edit.

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.