Is there a way to stop TD from going to the next line?

I am using Javascript to dynamically generate a table and I want it to extend beyond the boundaries of the page. When I manually create the table, it works fine and extends off the page, but once I put it into a loop, the <td> elements wrap onto a second line, creating multiple rows in the rendered HTML when they reach the end of the page.

<div id="panelindex" style="overflow:scroll;text-align:center;">
  <table border="0">
    <tr></tr>
  </table>
</div>

This code is within its own table (without any styling). Here is the Javascript code:

var q = Math.floor((1/numpanels)*500);
if(q>50) q=50;
panelindex.innerHTML = "<table border='0'><tr>"
for(i=0; i<numpanels; i=i+1)
{
panelindex.innerHTML = panelindex.innerHTML + "<td><div id='panel" + i + "' onclick='jumppage(" + i + ")' style='float:left;text-align:center;margin:8px;border-width:3;border-color:white;border-style:none;'><a href='#" + i + "'><img src='thumbnails.php?image=blowem" + zeroFill(i,2) + ".gif&GIF&tw=128&th=128&quality=" + q + "'>\n" + 
"<br />" + i + "</a></div></td>\n";
}
panelindex.innerHTML = panelindex.innerHTML + "</tr></table>"

You'll notice that there is a <div> inside each <td> so that I can add a border around the panels. Without this <div>, it doesn't seem to work properly. Any suggestions on how to keep all the <td> elements in one line instead of wrapping to a new line?

Desired Layout:

Current Issue: https://jsfiddle.net/w4uh0a3j/7/

Click the Show link.

Answer №1

When you set a string value to innerHTML, it is not simply holding that value.</p>

<p>innerHTML actually parses the value as HTML, creates a DOM from it, inserts it into the document, and then converts that DOM back into HTML when you read it back.</p>

<p>This process means that any errors in the string value can be corrected, like missing end tags.</p>

<p><div>
<div>
<pre class="lang-js"><code>If you try setting innerHTML more than once:</pre></div></div>

<p>You will notice the content being combined:</p>

<pre><code><table border="0"><tbody><tr></tr></tbody></table><td>etc etc

To avoid this issue, store your data in a separate variable and only assign it to innerHTML once your HTML structure is complete.

Instead of manually building HTML strings, consider using DOM methods like createElement and appendChild for a more robust approach.

Answer №2

Alright, I have the corrected HTML and JavaScript code for you. It appears that using innerHTML to update the HTML before all the necessary code has been built can cause issues with missing closing tags. Here is the updated code:

<div id="panelindex" style="overflow:scroll;text-align:center;">

</div>

And here is the revised JavaScript code:

var numpanels = 100;


var q = Math.floor((1/numpanels)*500);
if(q>50) q=50;
panelindex.innerHTML = "<table border='0'><tr>";
var html = "<table border='0'><tr>";
for(i=0; i<numpanels; i=i+1) {
html += "<td><div id='panel" + i + "' onclick='jumppage(" + i + ")' style='float:left;text-align:center;margin:8px;border-width:3;border-color:white;border-style:none;'><a href='#" + i + "'><img src='thumbnails.php?image=blowem" +  ".gif&GIF&tw=128&th=128&quality=" + q + "'>\n" +
"<br />" + i + "</a></div></td>";
}
html += "</tr></table>";
document.getElementById("panelindex").innerHTML = 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

Problem with alternating row colors in my table

Trying to add some style to my table on the webpage by alternating row colors, but I'm running into some issues. I've followed tutorials and tried using different CSS selectors like nth-of-type(even), nth-of-type(2n+0), and nth-of-type(odd). Howe ...

Refining search outcomes using multiple Multiselect boxes in VueJS

In my Vue component, I have successfully displayed results from a data object and created multiple multiselect boxes. However, I am facing challenges with filtering. I understand how to set and compare a single value from the multiselect for displaying sp ...

Move the element from the center of the screen back to its starting position

Looking to craft a single animation that will transition elements from the center of the current view back to their original positions. Unfortunately, CSS animations do not support toggling the display property. This limitation causes the second rectangle ...

I am in search of a specialized 3D camera with a unique effect

I am seeking assistance with animating a camera to replicate or closely resemble the effect seen in this demo: Any help would be greatly appreciated. ...

CoffeeScript:: I can't understand why the function body returns when using ajax

Hey there, I'm new to Coffeescript and have a question regarding Ajax. jQuery -> api = getId: -> res = [] $.ajax dataType: "jsonp" url: "http://localhost:3004/videos.json" success: (data) => ...

The script is unable to locate the property 'indexOf' because it is undefined

Searching for a specific value in an array using ui-select to capture values. A function is created to verify the existence of the value, which works perfectly fine. However, the console displays multiple instances of the error 'Cannot read property & ...

Unable to modify existing attributes in a sails.js model

I'm new to sails.js and I have a question about adding fields to an existing model in Sails.js. Here is the current model: module.exports = { attributes: { id: { columnName: 'id', type: 'integer&apos ...

Learn the process of uploading an image to Firebase storage from the server side

I'm working on implementing an upload feature that utilizes Firebase storage on the server side. Here is the upload function on the server side: const functions = require("firebase-functions"); const admin = require("firebase-admin&quo ...

Visualizing JSON data in React.js using Chart.js

Currently, as a beginner in ReactJS, I am working on an application that displays COVID-19 data from a JSON API in a visual format using Chart.js. However, despite trying various solutions, I am unable to visualize the data. Below is my source code: App ...

Which comment widget/platform does 9GAG utilize?

I am in the process of developing a new website and I am looking for a comment system to use. The comment system that 9GAG is currently using really catches my eye. Take a look at this random post as an example: Despite searching through their source code ...

Using CSS to customize the appearance of the preview in CKEditor

After successfully integrating CKEditor into my website CMS, I am facing an issue with the Preview button not using stylesheets (CSS). Despite editing the preview.html file located in: Website/ckeditor/plugins/preview/ It appears that the CSS is being ig ...

Printing specific div content from an HTML page using a print button

I have a situation where I need to control the printing functionality of my HTML page. Currently, I have two div tags in my code with a single print button at the top of the page. When I click on this button, it prints the content from both divs. However ...

Simple HTML files on a local server are having trouble loading images and JavaScript, yet the CSS is loading

Having worked in web design for quite some time, I recently began working on a basic Angular app that is meant to be very simple. For this project, I am only using the angular files and a single html file as the foundation. As I started setting up the bas ...

Using Jquery to change an id with the .attr method may not always produce the desired results

I have a div element that functions as a button when clicked, with the ID of knight. Every time I click on any of the 3 buttons (including knight), it changes the ID of knight to elf (yes, I'm creating a game). I have a hover effect for knight usi ...

Implementing onClick functionality in RecyclerView post JSON data extraction

I recently implemented a RecyclerView in a fragment and successfully parsed JSON data from a website to display it in the RecyclerView following a helpful tutorial found at: Now, my next challenge is adding an onClick listener to the items in the Recycler ...

Dynamic web designs can be achieved by utilizing a combination of technologies such as Ajax

Encountering issues while attempting to use Ajax and Fancybox.js photo viewer simultaneously. The website is initially set up as web 1.0 with standard navigation using hyperlinks. For html5 browsers, a JavaScript is utilized to create a web 2.0 experienc ...

When the source file is located in other directories, gcovr fails to generate a comprehensive report

I'm having trouble getting the correct HTML output from gcovr when the source file is located relative to my root directory. Let me illustrate with two cases where gcovr works and where it encounters issues: CASE:1 - Successful execution of gcovr Af ...

Dynamically assigning anchor tags to call JavaScript functions

Creating a list dynamically, full of <span class="infa9span"><img src="/csm/view/include/images/foldericon.png"/><a id="infa9Service">'+servicename+'</a><br/></span> tags. This list is then appended to a div. ...

Moving the webpage into the realm of mobile and tablet devices

Over the next few weeks, I will be working on transitioning my website content to have both a mobile and tablet version. During this process, I have some questions: What is the best way to redirect users to the correct site based on their device/browse ...

Is there a way to trigger a JavaScript function once AJAX finishes loading content?

I've been utilizing some code for implementing infinite scrolling on a Tumblr blog through AJAX, and it's been performing well except for the loading of specific Flash content. The script for the infinite scroll can be found here. To address the ...