-2

When the user clicks a button I want the below data to be displayed on my webpage in the following format:

car1 - id:1346 - type BMW

car2 - id: 1379 - type Holden

etc...

How exactly would I go about doing this? I tried this:

document.getElementById('message').innerHTML = car1

But all that was displayed was '[object Object]'

    var car1 = {id:"1346", type:"BMW"};
    var car2 = {id:"1379", type:"Holden"};
    var car3 = {id:"2580", type:"Ford"};
    var cararray = [car1, car2, car3];
1
  • You need to use JSON.stringify on an object to display it in text form. Commented Apr 25, 2017 at 11:04

5 Answers 5

0

I would suggest you using a bulleted list (ul) and just append elements from the cararray array.

var cararray = [{id:"1346",type:"BMW"},{id:"1379",type:"Holden"},{id:"2580",type:"Ford"}], 
    content = document.getElementById('message'),
    btn = document.getElementById('btn');

cararray.forEach(function(v,i) {
    var el = document.createElement('li');
    el.innerHTML = `car${i+1} - id: ${v.id} - type: ${v.type}`;
    content.appendChild(el);
});

btn.addEventListener('click', function() {
    content.style.display = 'block';
});
ul {
  list-style-type: none;
}
<button id='btn'>reveal list</button>
<ul id='message' hidden></ul>

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

Comments

0

You can access information of the car by using .<property>, where <property> in this case is either .id or .type.

To get all of the cars, you need to loop through cararray, and on each loop add it to the message.

var car1 = {id:"1346", type:"BMW"};
var car2 = {id:"1379", type:"Holden"};
var car3 = {id:"2580", type:"Ford"};
var cararray = [car1, car2, car3];

var message = '';

for (var i = 0; i < cararray.length; i++) {
    var car = cararray[i];
    
    message += "car" + (i + 1) + " - id:" + car.id + " - type " + car.type + "\n";
}

alert(message);

alert(message) is used as an example, you can replace this with document.getElementById('message').innerHTML = message, and use <br /> instead of \n when appending a car to the message.

5 Comments

Unsure why this got downvoted since OPs problem was displaying '[object Object]' when using "document.getElementById('message').innerHTML = car1". The car array wasn't part of the question
1: Don't use jsfiddle if you can just use snippets. 2: you're ignoring the fact the OP has multiple cars he wants to render.
@Cerbrus Answer changed and feedback noted. Thanks
Much better! Note that \n won't work if the OP wants to add this message to his HTML.
@Cerbrus Edited
-1

You have explicitly wright what variable you wan't to render, you can't transform an object to string. You can do this like this :

document.getElementById('message').innerHTML = "id:" + car1.id + " - type " + car1.type
//Return id:1346 - type BMW

or like this :

document.getElementById('message').innerHTML = JSON.stringify(car1)
//Return {"id":"1346","type":"BMW"}

Comments

-1

You can use a loop to iterate your array and display what you need

var car1 = {
  id: "1346",
  type: "BMW"
};
var car2 = {
  id: "1379",
  type: "Holden"
};
var car3 = {
  id: "2580",
  type: "Ford"
};
var cararray = [car1, car2, car3];

function display() {
  let message = "";
  cararray.forEach((car,i)=>message += "car"+(i+1)+" - id:"+car.id+" - type "+car.type+"</br>");
  document.getElementById('message').innerHTML = message;
}
<button onclick="display()">Display</button>
<p id="message"></p>

Comments

-1

    <html>
    <head>
    <script>
    function myFun()
    {
        var car1 = {id:"1346", type:"BMW"};
        var car2 = {id:"1379", type:"Holden"};
        var car3 = {id:"2580", type:"Ford"};
        var cararray = [car1, car2, car3];
        var cart="";
        for(var x=0; x<cararray.length; x++)
        {
          if(x==cararray.length)
            cart = cart + "car"+x+ "- id:"+cararray[x].id+" - type "+cararray[x].type;
          else
            cart = cart + "car"+x+ "- id:"+cararray[x].id+" - type "+cararray[x].type+ "<br>";
        }
        document.getElementById('message').innerHTML = cart;
    }
    </script>
    <head>
    <body onload="myFun()">
    <div id="message"></div>
    </body>
    </html>

Try this simple solution. hopefully, it will work for you!

14 Comments

May i know reason for downvoting ?
Divyesh: Imagine what this code would look like if you had 100 cars... Why aren't you using the cararray?
This solution just for this particular question sir.
i know looping concept but it might be complicated to understand for this kind of question
@Cerbrus OP didn't request that car array should be used. He gave an example, that wasn't working, and our solutions addressed the issue to this problem. Upvote for the correct solution
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.