Select from the available choices using the HTML5 select element with jQuery

I'm looking to create a simple jQuery plugin, but my current code only works in Firefox. How can I modify it to work in other browsers like Google Chrome, Opera, and others?

When selecting option value='business', the is--hidden class should be removed from the .address--company element. But when choosing option value='private', the is--hidden class should be added to the .address--company element.

HTML:

<div class="address--customertype">
  <div class="select-field">
    <select name="address[additional][customer_type]" required="required" aria-required="true" class="is--required">
      <option value="private" selected="selected">Klient indywidualny</option>
      <option value="business">Firma</option>
    </select>
  </div>
</div>
<div class="address--company is--hidden">...</div>

jQuery:

$('select[name="address[additional][customer_type]"] option[value="business"]').click(function() {
  if ($('.address--company').hasClass('is--hidden')) {
    $('.address--company').removeClass('is--hidden');
  }
});

Answer №1

Your code presents several key issues that need to be addressed:

  1. It is crucial to assign a change event to the select dropdown in order to properly listen for option changes.
  2. The jQuery selector being used is incorrect and needs to be adjusted.
  3. You should consider adding or removing classes based on the values of private and business.
  4. With only two options present, utilizing a simple if-else statement will suffice.
  5. Additionally, remember to include .trigger('change') to ensure proper class changes based on the selected default value.

$('select[name="address[additional][customer_type]"]').change(function() {
  if ($(this).val() === 'private') {
    $('.address--company').addClass('is--hidden');
  } else {
    $('.address--company').removeClass('is--hidden');
  }
}).trigger('change');
.is--hidden {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="address--customertype">
  <div class="select-field">
    <select name="address[additional][customer_type]" required="required" aria-required="true" class="is--required">
      <option value="private" selected="selected">Private Customer</option>
      <option value="business">Business</option>
    </select>
  </div>
</div>
<div class="address--company is--hidden">............</div>

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

jQuery classes fail to display effects when added

My HTML code is shown below: <div class="container"> <!-- Example row of columns --> <div class="row"> <div class="span7"> <div class="home-image left-image"> <img class="photograp ...

Update the background color of an li element upon clicking

Is there a way to change the background color upon clicking on the li anchors? <div class="text-center row"> <ul class="col-md-12 secondNav"> <li class="navbar-brand nav-item" style="margin-right: 9%"> <a href="#" >& ...

Successful jQuery Ajax request redirects user

ISSUE - Need help with redirecting on successful Ajax/JSON call. Is it possible to redirect to another page when an Ajax/JSON call is successful and a jQuery variable meets a certain value? Here's a brief code snippet: if(json.response.status == &a ...

Adjust the horizontal background-position transition for color change while scrolling using jQuery and CSS

I came across a fascinating website - - where the background-position changes on scroll() from a linear gradient with two colors to smoothly slide one color off the screen. While I grasped how they achieved the split colors using linear-gradient, I'm ...

Is there a way to create rectangles with a designated percentage of blue color using CSS or Bootstrap 3?

There's an image on my laptop that perfectly illustrates what I mean, but unfortunately, I'm not sure how to share it with you. In essence, I am looking to create a rectangle filled with a specific percentage of the color blue. Could you assist m ...

Is there a method to add a border around a specific table row?

Trying to add borders around specific table rows that change colors when the mouse hovers over them. However, borders are not visible unless using border-collapse:collapse;. I need to avoid border-collapse because it causes issues with visibility at the to ...

What specific CSS code do I need to incorporate in order to style a chat application?

I'm currently working on styling a simple chat application, nothing too extravagant, but I'm encountering a few obstacles. My styling goals include: Aligning blue messages (from you) to the right and gray messages (from other users) to the left. ...

Ensuring Proper Implementation of Tailwind CSS Styles on Next.js Image Component with Relative Positioning

Having some challenges while attempting to integrate the Next.js Image component into my React project alongside Tailwind CSS. Specifically, I'm facing issues with applying relative positioning and other styles like objectFit. It seems that the root c ...

Tips for declaring elements with equal width in a row using isolation-span

I have been experimenting with Singularity and trying to transition from the float to the isolation output style using grid- and isolation-span. However, there is one particular pattern that I am struggling to replicate. I wonder if there might be a more e ...

`CSS border issues`

I am currently attempting to create a square consisting of 4 smaller squares inside, but I have been facing challenges with the method I was using. Here is the code snippet: #grandbox { position: absolute; width: 204px; height: 204px; border: so ...

Creating perfectly spaced toast messages with CSS styling

I am trying to customize the alignment of my toast messages to the bottom left corner. Ideally, I want them to stack on top of each other with a small gap of 10px between each message, ensuring that they are all visible and readable. Currently, changing ...

The website was designed to only allow scrolling on desktop devices, not on mobile devices

I noticed that the website is not scrolling properly on mobile devices, specifically when viewed on a Samsung Galaxy S4 Mini. Does anyone have suggestions on how to resolve this issue? ...

Design a food selection with only CSS and HTML

I am working with a menu structure that looks like this: <ul class"menu"> <li> <a>item1</a> <ul> <li><a>subitem1</a></li> <li><a>subitem2</a></li> &l ...

Dynamic motion of balloons using jquery technology

I am trying to make an image move smoothly inside a div using jQuery and CSS. I have tried rotation and animation, but nothing has worked the way I need it to. <div class="baloon" style="position:fixed;left:0px;top:160px;"> <img id="rot" class="r ...

The text sliding feature gradually moves further away from the left side with each iteration

I am in the process of transferring an existing slider from one website to another. Instead of creating a text slider from scratch, I decided to modify the code I found for the new site. However, the slider is not functioning correctly on the new site. Th ...

Sending a parameter to multiple controllers simultaneously using an ActionLink in MVC4

Is it possible to pass a value from an action link to multiple controllers? In my current scenario, the Index page utilizes ajax requests to display 3 partial views. I have modified these partial views to include links to a report and now I want to pass t ...

Centered Menu with Left Alignment

I am trying to figure out how to align a sublist directly under a main list point that is centered. I have attached some images to show what I am aiming for: Currently, when I center the main list point (LEISTUNGEN), I am facing issues placing the sublist ...

Transform two arrays into a single array entity

I need to combine two separate arrays of the same length into an array object. This will make it easier to populate later on. For example: [1,2,3,4,5] ['a','b','c','d','e'] The desired output is: [{&apo ...

Tips for stopping the textarea from moving around as you type and avoid it hitting the bottom of the page: Utilize JQuery and

As I type into the auto-sizing textarea, the text runs to the bottom of the page and causes it to dance uncontrollably with each key press. This forces me to constantly scroll down to see what's happening. How can I prevent this page dancing and keep ...

Is an Ajax call live function needed?

In the sidebar of my website, there is a small box that gets populated using ajax. There are 2 ajax functions set up: one to add items to the list and the other to delete them when the user clicks on the delete button. The HTML structure for this list is q ...