Radio button selection with table layout

I am designing a quiz template with radio buttons. However, I would like them to be stacked vertically instead of side by side.

Why doesn't the <br/> tag work? I'm having trouble understanding it. Can someone assist me?

Here is my code:

http://jsfiddle.net/yyw2A/


<tr>
  <td colspan="2">
    <b>"Men are basically little piglets... Males are biologically driven to go out and hunt giraffes."</b>
  </td>
</tr>

<tr id="0">
    <td onmouseover="checkSelect(this)" onmouseout="setBGDefault(this)" onclick="applySelect(this,0)"><input type="radio" name=que_1 value="1" />Newt Gingrich</td>
    <br>
    <td onmouseover="checkSelect(this)" onmouseout="setBGDefault(this)" onclick="applySelect(this,1)"><input type="radio" name=que_1 value="0" />Dwight Schrute</td>
    <br>
    <td onmouseover="checkSelect(this)" onmouseout="setBGDefault(this)" onclick="applySelect(this,1)"><input type="radio" name=que_1 value="0" />Dwight Schrute</td>
    <br>
    <td onmouseover="checkSelect(this)" onmouseout="setBGDefault(this)" onclick="applySelect(this,1)"><input type="radio" name=que_1 value="0" />Dwight Schrute</td>
    <br>
</tr>

<tr id="correct" style="display:none">
  <td colspan="2">Correct! This quote is from a controversial lecture on the military that Gingrich delivered while teaching at Reinhardt College. He also made comments about women in combat missions.</td>
</tr>

<tr id="wrong" style="display:none;">
     <td colspan="2">Incorrect. This quote is from a controversial lecture on the military that Gingrich delivered while teaching at Reinhardt College. He also made comments about women in combat missions.</td>
</tr>

Answer №1

The br tag should not be placed within tr elements. To ensure that each checkbox appears on a separate line, you can group them all inside the same td element and enclose each input within a label:

<td ...>
    <label>
        <input type="radio" name=que_1 value="1" />
        Newt Gingrich<br>
    </label>
    <label>
        <input type="radio" name=que_1 value="0" />
        Dwight Schrute<br>
    </label>
    ...
</td>

In order to handle mouse events, it's recommended to attach them to each label instead of the td element:

<label onmouseover="checkSelect(this)"
       onmouseout="setBGDefault(this)"
       onclick="applySelect(this,0)">
    ...
</label>

If desired, you can eliminate the br tag using CSS styling:

td label {
    display:block;
}

Check out this JSFiddle demo for reference.

Answer №2

Instead of using a table for each radio button, consider using an unordered list (ul) with each radio button in a list item (li). Here is an example:

<ul id='0'>
   <lionmouseover="checkSelect(this)" onmouseout="setBGDefault(this)" onclick="applySelect(this,0)"><input type="radio" name=que_1 value="1" />Newt Gingrich </li>
     .
     .
     .
</ul>

If you prefer to use a table, you can place all radio buttons in one table cell (td) and add a br tag after each one.

Alternatively,

You can wrap each radio button in a table row (tr).

Answer №3

The reason for this issue is because the radio buttons are placed within a table structure. Each radio button is part of a column, which causes them to collapse if not properly formatted. To resolve this, each input should be enclosed in a tr tag as shown below:

<tr><td><input id="answer_1" type="radio" /><label for="answer_1">Answer 1</label></td></tr>
<tr><td><input id="answer_2" type="radio" /><label for="answer_2">Answer 2</label></td></tr>
<tr><td><input id="answer_3" type="radio" /><label for="answer_3">Answer 3</label></td></tr>

Instead of using a table, it is recommended to remove it entirely and use the following format:

<input id="answer_1" type="radio" /><label for="answer_1">Answer 1</label><br>
<input id="answer_2" type="radio" /><label for="answer_2">Answer 2</label><br>
<input id="answer_3" type="radio" /><label for="answer_">Answer 3</label><br>

Using the label element with inputs like checkboxes and radios not only improves accessibility for screen readers but also allows users to select the option by clicking on the text. You can test it out with this jsfiddle example: http://jsfiddle.net/8DLXw/

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

Get rid of the margins on your Wordpress theme

Currently, my Wordpress site is using the Tesseract theme which can be found at (http:// instantiwebs . com) I am interested in removing the left/right margins on all elements, similar to what has been done on this website: . Interestingly enough, they al ...

Centered on the screen are the input field and corresponding label

I am in the process of creating a signup form, and I have encountered an issue. How can I make the input wider without using a fixed width like width: 420px? Additionally, I would like to center both the input field and the label. I envision something simi ...

Error encountered when attempting to trigger a Bootstrap dropdown within a designated div

When using React JS, I encountered an issue when trying to trigger a dropdown that was nested inside a div with the class name "row". Initially, I placed the data-toggle and data-target attributes inside the div, like so: <div className="row"> < ...

Tips for overlaying text onto a MaterialUI icon

https://i.stack.imgur.com/cFr5Y.pngI am currently working on a project that requires me to overlay a number on top of an icon. Specifically, I am utilizing the MaterialUI ModeComment icon and attempting to display text over it. Despite trying the following ...

Eliminating the standard padding on a Vuetify v-app-bar

When using Vuetify's v-app-bar, there are default css classes named v-toolbar__content and v-toolbar__extension that apply padding of 16px on the x-axis and 4px on the y-axis, which I aim to eliminate. I attempted to override these classes in my CSS ...

Can you guide me on where to find the login with Facebook button on this site?

I am currently working on integrating a Facebook authentication system into my app, but I find the default log in button provided by Facebook developers section to be quite unappealing. My Question: I am curious about how websites like Stack Overflow man ...

Why won't my button's text resize?

I am facing an issue with my button's CSS code. In Chrome, when I resize the window to view the mobile layout, the text overflows outside of the div. a.syntra-green-button { background-color: #6faf3c; border: 1px solid #9dcc77; color: w ...

Customize the color of the horizontal line in React using dynamic properties

Is there a way to customize the color of an <hr /> element in a React application? If we were to use a functional component to accomplish this, how would we go about it? ...

Convert the HTML content into a PDF while retaining the CSS styles using either JavaScript or Django

Looking to create a PDF of an HTML element with background color and images. Seeking a solution, either client-side or server-side using Django/Python. Tried jsPDF on the client side, but it does not support CSS. Considering ReportLab for Django, but uns ...

Tips for displaying animations only the first time using HTML and CSS:

Upon the initial visit to my website, a captivating animation introduces itself with the words "Hello. I'm Bob" as it gracefully fades into view. Navigating through the menu bar allows users to explore different sections of the site. However, there ...

Restrict Bootstrap Column Width to Container Boundary

I am struggling to create a two-row hierarchy display for a set of photos using bootstrap 5. Despite my efforts, the images overflow out of the container instead of staying within it. My goal is to make the container fill the screen and act as a cover disp ...

Interactive text that changes when hovered over in an HTML document

My table contains text and I have implemented code that displays a small text when hovering over it. I would like the entire column to trigger the text display on hover, ideally in a larger size. Any suggestions on how to achieve this? ...

Is there a solution to move my navbar that is frozen halfway on the screen?

I'm in the process of developing a website and have implemented a sticky navbar using CSS. However, I've noticed that when I scroll down, the navbar appears to be "stuck" slightly further down the page (as shown in the second image). Can anyone p ...

The selection button's threshold

Welcome to my website design project! I have implemented image buttons for my web page and want to restrict the number of images a user can select. If a user tries to navigate away after selecting more than 3 images, I wish to display an alert message. Her ...

Determining the width of an element in terms of percentage

My issue involves input elements with widths set as percentages in CSS under the common class 'display-class' For example: input.test1 { width: 60% } In JavaScript, I am attempting to wrap a div around these input fields with the same width ...

Traversing tables with JQuery's nextUntil function

I have a calendar table where I need to highlight the range from the pickup date, maybe 10 Feb, to the set date of 20 Feb. <tr class="rd-days-row"> <td class="rd-day-body rd-day-disabled">09</td> <td class="rd-day-body">10& ...

Altering the character by striking a key

I have a canvas with custom styling. In the center of the canvas is a single letter. Please see the code below for reference. The goal is to change the displayed letter by pressing a key on the keyboard. For instance: Letter A is centered in the canvas: P ...

Modal - sending data through URL

I am trying to create a modal that will open another view using a URL in script, but instead it just opens a blank pop-up and doesn't redirect to the URL. I have attempted various approaches, but none seem to be working. Adding <div class="modal-b ...

Aligning text in the middle of a div vertically

Visit the Satoshi Faucet Index here I'm attempting to vertically align the text within the four divs containing sat, bit, mBTC, and BTC. I have tried using display:table-cell; vertical-align:middle; on the divs but unfortunately it isn't havi ...

Divide the PHP code into separate files

Can someone help me with a coding dilemma I have? I couldn't find any information on Google or this site about it. A friend of mine who has experience in website development and runs his own business uses an interesting system that is new to me. He ...