Updating contact list to a database table structure

Currently, my code displays the data in address books, but I would like it to be shown in a table instead. I attempted to use document.write to create the table, but I'm unsure how to populate the table with the data rather than the address book format. How can I achieve this?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN“ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Address Book</title>
  <style type="text/css">
      .ab {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: small;
    color: #993300;
    background-color: #CCFFCC;
    padding: 5px;
    height: 100px;
    width: 350px;
    border: thin dotted #993300;
    }
  </style>

  <script type="text/javascript">
    function addressBookItem (fname, lname, email) {
        this.fname= fname;
        this.lname = lname; 
        this.email = email;
    }
    var x = document.createElement("TABLE");
    addressBookItem.prototype.write = function() {
        // var adrbook = "<p class='ab' First Name: " + this.fname + "&ltbr /&gt";
        var adrbook = "<p class='ab'>First Name: " + this.fname + "<br />";
        adrbook += "Last Name: " + this.lname + "<br />";
        adrbook += "Email Address: " + this.email + "</p>";

        document.write(adrbook);
    }

</script>
</head>
<body>
    <script type="text/javascript">
        var aB1 = new addressBookItem('Roger', 'Williams', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e89a9f8184848189859ba88f85898184c68b87'); // [partial email address]'
        var aB2 = new addressBookItem ('Rose', 'Schultz', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99ebf6eafcc6ead9fcf8ebedf1f5f0f7f2b7f7fc'); // [partial email address]'
        document.write("<table border=\"2\"><tr><th>First Name</th><th>Last Name</th><th>Email Address</th></tr>");
        aB1.write();
        aB2.write();
        document.write("</table");
    </script>
</body>
</html>

Answer №1

    function createContact(fname, lname, email) {
        this.fname = fname;
        this.lname = lname; 
        this.email = email;
    }
    var table = document.createElement("TABLE");
    createContact.prototype.write = function() {
    
        var contactInfo = "<tr><td>" + this.fname + "</td>";
        contactInfo += "<td>" + this.lname + "</td>";
        contactInfo += "<td>" + this.email + "</td></tr>";

        document.write(contactInfo);
    }

    var contact1 = new createContact('Roger', 'Williams', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe8c89979292979f938dbe99939f9792d09d9193">[email protected]</a>');
    var contact2 = new createContact ('Rose', 'Schultz', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="addfc2dec8f2deedc8ccdfd9c5c1c4c3c683c3c8d9">[email protected]</a>');

    document.write("<table border=\"2\"><tr><th class='ab'>First Name</th><th class='ab'>Last Name</th><th class='ab'>Email Address</th></tr>");
    contact1.write();
    contact2.write();
    document.write("</table");
.contact-table {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: small;
    color: #993300;
    background-color: #CCFFCC;
    padding: 3px;
    height: 30px;
    width: 350px;
    border: thin dotted #993300;
}
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN“ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Address Book</title>
  </head>
<body>



</body>
</html>

Answer №2

Have you considered using HTML tables as an alternative?

var adrbook = "<table>
  <tr>
    <th>First Name</th>
    <th>Last Name</th> 
    <th>Email</th>
  </tr>
  <tr>
    <td>" + this.fname +" </td>
    <td>" + this.lname + "</td> 
    <td>" + this.email + "</td>
  </tr>
</table>"

It's worth considering if this fits your needs, and it could potentially be a viable solution.

Answer №3

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN“ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Contact List</title>
  <style type="text/css">
      .cl {
    font-family: Arial, sans-serif;
    font-size: medium;
    color: #336699;
    background-color: #FFFFCC;
    padding: 8px;
    height: 120px;
    width: 300px;
    border: thin solid #336699;
    }
  </style>

  <script type="text/javascript">
    function contactListItem (firstname, lastname, emailaddress) {
        this.firstname = firstname;
        this.lastname = lastname; 
        this.emailaddress = emailaddress;
    }
    var y = document.createElement("TABLE");
    contactListItem.prototype.write = function() {
        // var contacts = "<p class='cl' First Name: " + this.firstname + "&ltbr /&gt";
        var contacts = "<tr><td>" + this.firstname + "</td>";
        contacts += "<td>" + this.lastname + "</td>";
        contacts += "<td>" + this.emailaddress + "</td></tr>";

        document.write(contacts);
    }

</script>
</head>
<body>
    <script type="text/javascript">
        var cl1 = new contactListItem('Alice', 'Johnson', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0c2c7d9dcdcd9d1ddc3f0d7ddd1d9dc9ed3dfdd">[email protected]</a>');
        var cl2 = new contactListItem ('Bob', 'Smith', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90e2ffe3f5cfe3d0f5f1e2e4f8fcf9fefbbefef5e4">[email protected]</a>');
        document.write("<table border=\"2\"><tr><th>First Name</th><th>Last Name</th><th>Email Address</th></tr>");
        cl1.write();
        cl2.write();
        document.write("</table");
    </script>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can AngularJS utilize ng-repeat and ng-bind-html to display arrays of HTML strings?

I'm currently attempting to display HTML strings from an array of HTML strings in my AngularJS project. As a newcomer to AngularJS, I have been trying to follow the examples provided on the official AngularJS website, but I am struggling to find a sui ...

How can you utilize a previously opened window from another page in JavaScript?

One of my challenges involves managing windows in a JavaScript environment. When I open a child window using window.open("http://foobar","name"), it reuses the same window when opened with the same name, which is exactly what I want. However, if the origi ...

What are the steps to design a versatile gallery page that can serve various projects?

Allow me to get straight to the point. What is my goal? I am aiming to develop galleries for various projects. Each project's thumbnail on the main page should open a gallery as a new page. These galleries will all have the same layout but different ...

Updating an Element in an Array with Mongoose

Is there a more efficient way to update an existing element in an array without fetching the database multiple times? If you have any suggestions, I would greatly appreciate it. Thank you! const updateStock = async (symbol, webApiData) => { try { ...

Executing an Ajax request to a document containing <script> elements

Recently, I developed a sample page that effectively mimics table layout pages but without relying on the traditional mobile-unfriendly table features. The structure of the page is as follows: <html> ... <body> <div type="page" ...

What to do when IE6/IE7 margins disappear after moving a relatively positioned floated div using jQuery's .hover()?

Sharing my code with you: http://jsbin.com/uhera3/edit Encountered an issue where the relatively positioned floated divs move to the left in IE7 when using jQuery's .hover() function. Any insights on how to resolve this problem? ...

An issue arises when data from the parent ajax call is being referenced

I am encountering a situation where I have nested ajax functions within each other. $.ajax({ url: '../...', type: 'POST', dataType: 'JSON', ...

Error 404: "Headers already sent to the client cannot be modified"

I'm currently developing a Node/Express application and I want it to display a 404 page if a promise function doesn't resolve. This is my approach: app.get("/projects", t("projects", function(req, res) { res.header("Cache-Control", "private ...

When I implement JavaScript on my website, my HTML content does not show up

Whenever I try to fetch content from a specific URL using AJAX and a script, my HTML content does not show up. In other words, the table element is not displayed on the page. Instead, I am able to retrieve data from the specified URL and display it in an a ...

Choose class based on the retrieved information

Good morning, I am working on dynamically setting the class of a td element based on data fetched from a database to reflect any changes or updates. One of the fields returned has four possible options, and I need to modify the CSS class of that field acc ...

Rendering a dynamic list of asynchronous components in Vue 3, with support for extension

Could you please assist me in resolving this issue? I've spent countless hours searching for a solution, but I can't seem to make it work. Vue is still very new to me. Let me provide some more context. I have an asynchronous component that i ...

angularjs select not chosen option

Hey there, I'm currently attempting to select an item from an array in the select options by using a string value. Below is my HTML code: <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularj ...

Employ gulp for rebasing CSS URLs

I have a question regarding how to resolve the following issue: In my sass files, I utilize absolute path variables in my main include files. For example, fonts are stored in /assets/fonts/... and icons are located in /assets/icons/... These paths only wo ...

Utilize jQuery to swiftly align elements within a designated container

Check out my JSFiddle example: http://jsfiddle.net/c62rsff3/ I'm attempting to implement snapping behavior between 2 elements using this code: $('.draggable-elements').draggable({ snap: true }); In addition, I've defined a container ...

Error message in Ionic 2: "Property is not found on type"

Currently, I am working on a project in Ionic 2 and have encountered a stumbling block with a seemingly simple task. My issue lies with a Textbox where I aim to input text that will then be displayed. I found some code on a website (http://www.tizag.com/j ...

Iterate through a JavaScript grid and display the items

I am attempting to iterate through a JavaScript table, aiming to display the elements located between two selections made by the user. Here is an example of the table structure: if the user selects IDs 2 and 5, the loop should display rows 3 and 4. I trie ...

Establish a comprehensive background for your Angular project

I've been struggling to figure out how to set a background image for my entire angular project. I've tried using global css and the app.component.css file, but it only sets the background for the component area. Here is a snippet from my global ...

Navigating a jQuery collection using iteration

My goal is to loop through an array and set values to an element in the following manner: <tool>hammer</tool> var tools = ["screwdriver", "wrench", "saw"]; var i; for (i=0; i < tools.length; ++i){ $("tool").delay(300).fadeOut().delay(100). ...

Changing the background color of a PHP input based on the webpage being viewed - here's how!

I'm in the process of creating a website where each page will have its own unique background color. Additionally, I am using a PHP input for both the header and footer sections, which need to change their background colors based on the specific webpa ...

Eliminating repeating entries in autocomplete results JSON

I am facing a challenge where I have integrated two feature classes, resulting in occasional duplication of results in the autosuggest feature. I am exploring options to detect and display alternatives instead of repeating the same result twice. Check out ...