Conflict between JavaScript function and CSS styling

I'm facing an issue with a radio button in a large form that has some styling applied to it. The background color changes when hovered or checked, but there seems to be a conflict with an important JavaScript function in the rest of the form. It took me a while to realize that the problem stems from this function, and now I'm stuck on how to resolve it.

Here's the code snippet causing the conflict:

$("#general-form").on("click", "label", function() {
  name_input = $(this).children("input").attr("name");
  if (name_input) {
    onglet = obj_critere_form.simulation_encours;
    $("#simul_" + onglet + " input[name='" + name_input + "']").focus();
  }
  return false
});

obj_critere_form = new critere_form();
obj_critere_form.initialize();
#general-form .radio-toolbar input[type="radio"],
p {
  display: none;
}

#general-form .radio-toolbar label {
  display: inline-block;
  background-color: #ddd;
  padding: 4px 11px;
  font-family: Arial;
  font-size: 16px;
  cursor: pointer;
}

#general-form .radio-toolbar label:hover {
  background-color: #bbb;
}

#general-form .radio-toolbar input[type="radio"]:checked+label {
  background-color: #bbb;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="general-form">

  <div class="radio-toolbar">
    <h2>Options:</h2>
    </br>
    <input type="radio" id="radio1" name="radios" value="a1">
    <label for="radio1">option 1</label>

    <input type="radio" id="radio2" name="radios" value="a2">
    <label for="radio2">option 2</label>

    <input type="radio" id="radio3" name="radios" value="a3">
    <label for="radio3">option 3</label>
  </div>

</div>

Answer №1

The issue lies in the inclusion of return false; within the click handler function. This effectively acts as event.preventDefault();. When a label is clicked, the default behavior is to trigger a click on the associated element specified by the for attribute; by using return false;, this action is prevented, resulting in the button not being checked when the label is clicked.

It seems unnecessary to have this line in the click handler function in the first place as it essentially disables clicking on the label.

$("#general-form").on("click", "label", function() {
  name_input = $(this).children("input").attr("name");
  if (name_input) {
    onglet = obj_critere_form.simulation_encours;
    $("#simul_" + onglet + " input[name='" + name_input + "']").focus();
  }
  //return false
});

//obj_critere_form = new critere_form();
//obj_critere_form.initialize();
#general-form .radio-toolbar input[type="radio"],
p {
  display: none;
}

#general-form .radio-toolbar label {
  display: inline-block;
  background-color: #ddd;
  padding: 4px 11px;
  font-family: Arial;
  font-size: 16px;
  cursor: pointer;
}

#general-form .radio-toolbar label:hover {
  background-color: #bbb;
}

#general-form .radio-toolbar input[type="radio"]:checked+label {
  background-color: #bbb;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="general-form">

  <div class="radio-toolbar">
    <h2>Options:</h2>
    </br>
    <input type="radio" id="radio1" name="radios" value="a1">
    <label for="radio1">option 1</label>

    <input type="radio" id="radio2" name="radios" value="a2">
    <label for="radio2">option 2</label>

    <input type="radio" id="radio3" name="radios" value="a3">
    <label for="radio3">option 3</label>
  </div>

</div>

Answer №2

It appears that a form library may be utilized in this scenario, possibly with its own unique styling.

When incorporating custom CSS to override the existing styles, exercise caution and refrain from excessive use of the !important declaration.

//example
color: red !important;

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

Deactivate button after a set period of time

Using angularjs, I have a button to create tickets that works with an http request to call a PHP web service. The problem is that the http request has a delay of x seconds, allowing users to click multiple times during this delay resulting in multiple ti ...

Leveraging Node.js to efficiently handle large Excel files within a SAP S/4HANA application

I am seeking assistance with a challenge I am currently facing (please note that I am not a JavaScript developer). Any help would be greatly appreciated. Procedure: The user uploads an Excel file from the UI. In the backend, we utilize xlsx/exceljs Java ...

Tips for customizing the background color of hyperlinks

Hello, I am trying to figure out how to create a hyperlink with alternating background colors. Can anyone guide me on achieving this? <div class="left_column"> <div class="category"> <p>Category</p> </div> <ul ...

Filtering with ReactJS can be made more powerful by

I have a list of data here: const UserData = [ { "id": 1, "name":"John", "age":30, "keywords": ["USA","Canada"] }, { "id": 2, "name":"Anna", "age":25, "keywords": ["France"] }, { "id": 3, "name":"Michael", ...

The query does not produce a match when using the LIKE operator with the retrieved ajax data

My goal is to sync up profiles in my MySQL database with the names and skillsets dropped into my droppable div. At this link, you can find a snippet of code for reference. I am facing two main issues - firstly, the error message mysql_fetch_array() expects ...

Obtain image data from a websocket server built in C# or VB.NET

Just starting out in the world of websockets, I've got a handle on the client-side code (JavaScript makes it easy) But diving into the websocket server side, particularly in c#, is a whole different ballgame. I'm in need of websocket server code ...

Vue select component not refreshing the selected option when the v-model value is updated

Trying to incorporate a select element into a Vue custom component using the v-model pattern outlined in the documentation. The issue at hand is encountering an error message for the custom select component: [Vue warn]: Avoid directly mutating a prop as i ...

Leveraging Fetch in React for retrieving information from a server

Attempting to retrieve data from the server (using Node.js) using the code below: componentDidMount = () => { fetch('http://localhost:3000/numberofjobs') .then(response => response.json()) .then(numberOfJobs => { console.log(numbe ...

Adjust the background color and transparency of a specific section using HTML

Is there a way to modify the background color or opacity of a specific area within an image? Take a look at my HTML, JavaScript, and CSS: function changeColor() { document.getElementById('testid').setAttribute("class", "style1"); } ...

Launching the Node.js Thread Pool to Enhance Parallel Processing

Hey there! I recently discovered that Node.js operates on a single thread. In order to optimize the performance of my application (MongoDB/Express), I've come up with the following script to utilize all 8 processors: #!/bin/bash node app.js & nod ...

The jQuery Slider smoothly shifts content downward and locks it in place

I have implemented a jQuery slider panel that pushes content down instead of overlapping it, and while it works well on Chrome, I am encountering issues on Firefox. In Firefox, when the panel is closed, the pushed-down content remains in place, creating a ...

Icon overflowing due to Bootstrap 4 card title

I'm currently working on implementing an expand/collapse feature with a red close 'X' button in a Bootstrap 4 card. I've almost got it working, but when I try to place the red close icon outside the element that controls the expand/coll ...

Troubleshoot: Border problem within nested columns in Bootstrap

https://i.sstatic.net/RDWfp.png My layout is based on the bootstrap 4 grid system, as shown in the image above. The problem I'm facing is that the border at the bottom row is misaligned. I have four nested columns in the first and second rows, but on ...

Guide on setting default attributes for all properties of an object at once

Currently, I'm in the process of developing an AngularJS service provider (function) that achieves the following objectives: Gathers data from multiple tables within an SQLite database Delivers the resulting object to various controller functions S ...

What is the best way to retrieve the ID of a data from a modal and subsequently send it to an ajax function?

I am currently developing a system that includes a CREATE EVENT module. This module allows users to create events and showcase them on their profile, where other users can join the events. To provide more detailed information about an event, I have incorpo ...

calculating the rotation angle from the origin to a vector within a three-dimensional space using three.js

I am working with a vector in three-dimensional space. Is there a method to determine the angle of rotation from the origin to this vector on each axis? For example, if the vector is located at x=6, y=-10, z=20, what angle does the vector make with resp ...

Conducting AJAX Verification Before Submitting a Form Using JavaScript

Currently, I have a basic form for new user sign-up. My goal is to prevent users from submitting the same email address twice. Therefore, I am considering implementing an Ajax check prior to form submission to verify if the email already exists in the syst ...

Horizontal rule spans the full width of the content, appearing within an ordered list

Check out this example to see the issue with the horizontal rule not extending all the way across under the number. I've tried using list-style-position:inside;, but that interferes with positioning the number correctly due to the left image being flo ...

A Guide to Modifying Background Image Attribute using JavaScript

I am currently in the process of changing an attribute of a JavaScript variable from url("../images/video.png") (as declared in the CSS) to url("../images/pause.png") using this line of code: fullscreenPlayPauseButton.style.backgroundImage = "url("../imag ...

Encountering a CORS error in my Next.js 13.4 application while attempting to retrieve JSON data

Here is the location of the actual fetch request in our search/page. import { useSearchParams } from "next/navigation"; import Footer from "../components/Footers"; import Header from "../components/header"; import { format } ...