Javascript What is wrong?

0

I have written this code and except for the header, nothing else comes out. Where is the / the failures?

<!DOCTYPE html>
<html lang="es">
<body> 
<h2>JavaScript document.write & document.getElementById.innerHTML Methods</h2>

<p id="output1" ></p> 
<p id="output2" ></p> 

<script>
var myObj = [firsname : 'John', lastname : 'Doe'];
document.write(" " + myObj);
document.write("hello world");
document.getElementById("output1").innerHTML = "" + myObj;
document.getElementById("output2").innerHTML = "Hello World";
</script>

</body>
</html>
    
asked by csvicuna 06.06.2018 в 21:20
source

2 answers

4

The syntax of your object is wrong, to create objects use { and not [

<!DOCTYPE html>
<html lang="es">
<body> 
<h2>JavaScript document.write & document.getElementById.innerHTML Methods</h2>

<p id="output1" ></p> 
<p id="output2" ></p> 

<script>
var myObj = {firsname : 'John', lastname : 'Doe'};
document.write(" " + myObj);
document.write("hello world");
document.getElementById("output1").innerHTML = "" + myObj;
document.getElementById("output2").innerHTML = "Hello World";
</script>

</body>
</html>
    
answered by 06.06.2018 в 21:22
-1

To add, you are including an object within the DOM, you must access the attribute of each object so that the info it contains is visible, there:

<script>
var myObj = [firsname : 'John', lastname : 'Doe'];

.

document.getElementById("output1").innerHTML = "" + myObj.firsname +" - "+ myObj.lastname;

...

    
answered by 07.06.2018 в 16:23