Arranging buttons that are generated dynamically in a vertical alignment

Currently, I am in the process of creating a webpage for various items, and everything is going well so far. However, I have encountered an issue while attempting to vertically align the buttons that I am generating using JavaScript within the document.

I have experimented with multiple approaches, such as applying the display style: block; property extensively, but unfortunately, the buttons are not cooperating.

Although I managed to develop a function where the buttons were integrated into a list structure, they lost their button appearance in the process (even though they still retained functionality), prompting me to abandon that solution.

Oddly enough, JSFiddle does not display the buttons properly. Here is the code I am currently working with:

https://jsfiddle.net/jets29sy/8/

// JavaScript code goes here...
# CSS code goes here...
// HTML code goes here...

When previewing the page locally, everything seems to be displaying correctly except for one thing - the buttons are aligned horizontally instead of vertically. At this point, the positioning of the buttons container is not my immediate concern; rather, I am focused on finding a way to vertically align the buttons within the container using JavaScript, HTML or CSS.

Answer №1

How about this:

#foobar {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

Answer №2

Adding content directly inside the div tag is sufficient, there is no need for an additional span tag within the div (for buttons) and applying CSS to make sure the button has a display: block;.

DEMO

var URLobj = {
   linkname1: "https://stackoverflow.com/",
   linkname2: "https://www.google.com",
   linkname3: "https://en.wikipedia.org/"
};

function exitbutton(){

   var textelement = document.getElementById("text");
   textelement.style.display = "block";

   var buttonelement = document.getElementById("buttons");
   buttonelement.style.display = "block";

   var headerelement = document.getElementById("header");
   buttonelement.style.display = "block";

   var iframe = document.getElementById("iframeID");
   iframe.src = "#";
   iframe.style.display = "none";

   var exitbutton = document.getElementById("buttonID");
   exitbutton.style.display = "none";

}

function iframeLoaded(){
   var iframeID = document.getElementById("iframeID");
   iFrameID.height = 0;
   iFrameID.width = 0;

} // add to game // parent.iframeLoaded();

function add(name, URL) {
   var element = document.createElement("button");

   //Assign different attributes to the element.
   element.setAttribute("value", URL);
   element.innerHTML = name;
   element.onclick = function () {
      document.getElementById("iframeID").src = this.getAttribute("value");
      document.getElementById("iframeID").width = "95%";
      document.getElementById("iframeID").height = "95%";
      document.getElementById("iframeID").style.display = "block";

      var exitbuttonelement = document.getElementById("buttonID");
      exitbuttonelement.style.display = "block";

      var textelement = document.getElementById("text");
      textelement.style.display = "none";

      var buttonelement = document.getElementById("buttons");
      buttonelement.style.display = "none";

      var headerelement = document.getElementById("header");
      buttonelement.style.display = "none";
   };

   var foo = document.getElementById("buttons");

   //Append the element in page (in span).
   foo.appendChild(element);
}

window.onload = function() {
   for (var key in URLobj) {
      if (URLobj.hasOwnProperty(key)) {
         add(key, URLobj[key])
      }
   }
};
#buttons {
   vertical-align: right;
   float: right;
   display: block;
}
#foobar {
   display: block;
}
#text {
   vertical-align: right;
   float: left;
   text-align: left;
}

#buttonID {
  float: right;
}

.btn-group button {
    cursor: pointer;
    display: block;
}
<head scroll="no">
<link rel="stylesheet" href="page.css">
<script type="text/javascript" src="page.js"></script>
</head>
<body scroll="no">
<div id="header", style="block"><h2>Your links are located here.</h2></div>
<div class="btn-group" id="buttons"></div>
<div id="exit"><button type="button" id="buttonID" style = "display: none" onclick=exitbutton()>Exit link!</button></div>
<div class = "frame_div", background-color="blue">
    <iframe id="iframeID", width="0", height="0", align="middle"></iframe>
  </div>
<div class = "text", id="text", style="block">
<tr>
<th>Choose your link from the list.<br></th>
</tr>
</div>
</body>

Answer №3

Using "display: block" could have fixed the issue you were experiencing.

Upon reviewing your code, it appears that you applied the style to the incorrect location. You placed it within the <div id="buttons">, but it should have been added to each individual <button> element within the div.

https://i.sstatic.net/fPSAW.png

To achieve the desired outcome, make sure to include the style on those specific elements.

https://i.sstatic.net/VlJNH.png

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

Combining the Powers of ExpressJS and MeteorJS

I am currently using an Epress JS rest api to send data to a MongoDB database. I'm curious if it's feasible to incorporate Meteor JS for fetching these values from the MongoDB. Any insights or suggestions would be greatly valued. Thank you ...

Exploring the utilization of server-side Web Sockets functionality

Recently, I've been diving into the world of Web Sockets and I'm eager to understand how server-side implementation works. While I feel comfortable with writing client-side code using onmessage() in HTML5, JavaScript, etc., I find myself unsure a ...

Error Alert: Invalid Hook Call detected while using the useSelector hook within a Functional Component

I am encountering an error Error: Invalid hook call. Hooks can only be called inside of the body of a function component. The versions of React and the renderer (such as React DOM) might not match There could be violations of the Rules of Hooks Multiple ...

Leveraging the power of the 'var' keyword in JavaScript when

I have a javascript code snippet that looks like this: var grade_type = document.getElementById("grade_type").value; gradesRef.set({ grade_type: firebase.firestore.FieldValue.arrayUnion(grade) }); However, when the data is stored i ...

What steps do I need to follow to change the background color to white for the <App/> component in solid-js?

In my solid-js application styled with tailwindcss, I established a gray color in the index.css for the background of various screens. However, I wanted the main <App/> component to have a white background instead. To achieve this, I created an App.c ...

Creating HTML tables with PHP for dynamic web content

I am facing a minor issue while displaying my results in tables. Here is the code snippet: <?php //return data if any while ($client = mysqli_fetch_assoc($result)) { //output data form each client //var_dump($client); ?> < ...

Trouble with shadow rendering in imported obj through Three.js

After importing an object from blender and setting every mesh to cast and receive shadows, I noticed that the rendered shadows are incorrect. Even after merging the meshes thinking it would solve the issue, the problem persisted. It seems like using side: ...

Using data across multiple instances in node.js EJS partials

I need some advice regarding my nodejs project. I have created a sidebar partial that requires user data to be displayed on it. This sidebar is utilized across multiple pages in the project. However, currently, I have to include the user data in every func ...

Unable to adjust the height of an MP4 video to properly display within a Material Box in both landscape and portrait orientations

While I have been learning JavaScript React, I encountered an issue with positioning an MP4 movie. You can view the code in this Codesandbox If you check out the FileContentRenderer.jsx file, you will see that the html5-video is used for the MP4. The g ...

Executing a JavaScript function within Python using Selenium: A beginner's guide

Within my JavaScript, there is a function called 'checkdata(code)' which requires an argument named 'code' to execute and returns a 15-character string. I recently discovered how to call functions without arguments in JavaScript. Howev ...

JavaScript parsing error occurred

Encountering a parsing error in my JavaScript code when deploying Firebase functions. The error mentions an unexpected token, indicating there might be a character out of place. I've been stuck on this issue for weeks now. Any assistance would be grea ...

Navigating a webpage with an HTML5 canvas in a horizontal direction

My canvas is sized at 500x200 and I am drawing blocks within it, each with a fixed size of 100x50. Through AJAX, I receive information about how many blocks to draw, but because the canvas can only display 5 blocks horizontally and 4 vertically, what happe ...

What are the best practices for managing promises effectively?

Currently, in my Angular application, I am utilizing $odataresource for handling data retrieval and updates. The code snippet I am working with is as follows: var measure = $odataresource("http://windows-10:8888/ChangeMeasure/"); var myMeasure = measure ...

The Vue warning states that the property "$store" was attempted to be accessed during rendering, but it is not defined on the current instance

Hello, I am new to the world of web development and currently working on an online shop project. I have encountered two errors that I'm struggling to fix: 1) [Vue warn]: Property "$store" was accessed during render but is not defined on instance. 2) ...

The code encountered a parsing error at 11:1, due to the presence of

I'm having trouble with this code. I've searched for answers but haven't found any solutions. Here is the error message: 11:1 error Parsing error: Unexpected token } ✖ 1 problem (1 error, 0 warnings) npm ERR! code ELIFECYCLE npm ERR! ...

Creating unique random shapes within a larger shape on a canvas, as shown in the image

I have a parent rectangle and would like to add up to 10 or fewer rectangles on the right-hand side corner of the parent rectangle, as shown in the image below: https://i.sstatic.net/RW9ix.png I attempted to write code to achieve this, but the alignment ...

The html wrapping around the jQuery JSON Response adds an extra layer of presentation

Seeking JSON data for an autocomplete function, but running into a problem because the JSON is wrapped in an HTML span tag. This issue stems from a VB backend library that won't be updated anytime soon. I need to find a way to remove the span tag from ...

Implement Javascript Event Listener

I'm a newcomer to the world of javascript and I’m struggling to understand why the code below isn't functioning correctly: var displayMessage = document.getElementById("notification"); displayMessage.addEventListener("click", function() { ...

I am currently working with CakePHP and am interested in learning how to expire the admin session

I'm having issues with the code in my UserController. When I open the admin panel in two tabs within the same browser and log out from one tab, I am unable to redirect to the login page from the other tab when clicking on any menu. function beforeFil ...

How can I uniquely identify and edit individual cells within an ng-grid using the bgColor property?

I need some clarification regarding Angularjs ng-grid. Is there a way to distinguish between the edit-cell and non-edit-cell in ng-grid? Perhaps using different background colors? ...