Objects data printed in different divs

var emp_info = {
emp1:{
name : “tom”,
email: “tom@gmail.com”,
contact : 737328738,
},
emp2: {
name : “john”,
email: “john@gmail.com”,
contact : 637788738,
},
emp3:{
name : “selena”,
email:"selena@gmail.com",
contact : 8736728738,
}
};
how to print the 3 different objects data of employes in three different divs created in html . we have to print empl1 in div 1 empl2 in div 2 empl3 in div 3

// Get the three div elements

var div1 = document.getElementById(“div1”);

var div2 = document.getElementById(“div2”);

var div3 = document.getElementById(“div3”);

// Access the employee objects

var emp1 = emp_info.emp1;

var emp2 = emp_info.emp2;

var emp3 = emp_info.emp3;

// Create HTML elements to display the employee information

var emp1Name = document.createElement(“p”);

emp1Name.innerHTML = "Name: " + emp1.name;

var emp1Email = document.createElement(“p”);

emp1Email.innerHTML = "Email: " + emp1.email;

var emp1Contact = document.createElement(“p”);

emp1Contact.innerHTML = "Contact: " + emp1.contact;

// Add the HTML elements to the first div

div1.appendChild(emp1Name);

div1.appendChild(emp1Email);

div1.appendChild(emp1Contact);

// Repeat the process for the second and third employees

var emp2Name = document.createElement(“p”);

emp2Name.innerHTML = "Name: " + emp2.name;

var emp2Email = document.createElement(“p”);

emp2Email.innerHTML = "Email: " + emp2.email;

var emp2Contact = document.createElement(“p”);

emp2Contact.innerHTML = "Contact: " + emp2.contact;

div2.appendChild(emp2Name);

div2.appendChild(emp2Email);

div2.appendChild(emp2Contact);

var emp3Name = document.createElement(“p”);

emp3Name.innerHTML = "Name: " + emp3.name;

var emp3Email = document.createElement(“p”);

emp3Email.innerHTML = "Email: " + emp3.email;

var emp3Contact = document.createElement(“p”);

emp3Contact.innerHTML = "Contact: " + emp3.contact;

div3.appendChild(emp3Name);

div3.appendChild(emp3Email);

div3.appendChild(emp3Contact);

Please try this.

1 Like

done mam thank you mam

for(var x in emp_info.emp1){
var s=emp_info.emp1[x]
console.log(s)
document.getElementById(“div1”).innerHTML +=s+ “
”;
}

for(var x in emp_info.emp2){
var s=emp_info.emp2[x]
console.log(s)
document.getElementById(“div2”).innerHTML +=s+ “
”;
}

for(var x in emp_info.emp3){
var s=emp_info.emp3[x]
console.log(s)
document.getElementById(“div3”).innerHTML +=s+ “
”;
}

2 Likes

Your Welcome!
Have a Happy Learning.