What steps should I take to trigger a notification when no selection has been made?

When the product size is not set, the text "Must pick 1 size for the product" should be displayed. However, I am facing an issue with displaying this text. Can someone guide me on how to properly use the select and input elements?

<select name="product_size" class="form-control" required oninput="setCustomValidity('')"
oninvalid="setCustomValidity('Must pick 1 size for the product')">
    <option disabled selected>Select a Size</option>
    <option>Small</option>
    <option>Medium</option>
    <option>Large</option>
</select>

enter image description here

Answer №1

Welcome to StackOverflow!

Your code has a couple of issues that need addressing.

  1. The problem with the oninvalid callback is that it was never triggered because there was no validation check on the select element. To fix this, you should enclose the select in a form element and include a <button> within it. When the user clicks this button, the browser will validate the form and if the select is invalid, your oninvalid code will be executed.

  2. Your code works perfectly on Firefox after adding the form wrapper, but it doesn't work on Chrome. To address this, I made the default option appear invalid by adding the value="" attribute to it.

Here's a corrected example:

<form action="javascript:void()">

  <select 
    required 
    name="product_size"
    class="form-control"
    oninput="setCustomValidity('')"
    oninvalid="setCustomValidity('Must pick 1 size for the product')"
  >
      <option value="" disabled selected>Select a Size</option>
      <option>Small</option>
      <option>Medium</option>
      <option>Large</option>
  </select>

  <button type="submit">Submit</button>
</form>

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

Empty Textarea Issue on iOS Safari on Mobile Device

I've encountered an issue with a textarea element in a form. After clicking on a button to show the form (which includes dynamic HTML), I can see the textarea element with all its styles applied. However, when I try to type in the textarea on iOS Saf ...

How to create a custom Angular 2+ directive for detecting a reset function on an input element

I developed a unique directive for input elements, specifically a search directive that filters items based on the search string provided by the user. <input pieSearch placeholder='Search' [attribute]='"label"' [formControl]='s ...

When utilizing the position: absolute property, scrolling down my content becomes challenging

I have encountered a challenging issue that I am struggling to resolve. Currently, I am working on developing a left side menu using jQuery and I have managed to get it up and running. However, the problem arises when I set my div #content with only posit ...

The movement of the Bootstrap navbar brand causes a shift in adjacent elements

Is there a way to use the bootstrap navbar brand without it affecting the layout of other elements? I'd like to have the icon aligned to the left and the navbar elements perfectly centered. Alternatively, is there a way to include the element as a n ...

How to restrict CSS div from expanding with content and implement scrollbars instead?

Can anyone assist me with this issue? I am struggling to make the div (the gray area behind the video) expand with the content, instead I keep getting scrollbars. Any suggestions would be greatly appreciated. Thank you, Marcus If you try scrolling over t ...

An error occurs when trying to modify the classList, resulting in an Uncaught TypeError for setting an indexed property

I am attempting to modify the classes of multiple sibling elements when a click event occurs. Some of these elements may have multiple classes, but I always want to change the first class. Below is the code that I created: let classList = event.currentTa ...

JS Code from a JS Fiddle failing to run in a web browser examination

I have been experimenting with a fantastic Expand/Collapse JavaScript function that I came across on JS Fiddle, but unfortunately, it's not working as expected when testing it in a browser. Can anyone provide some guidance on what might be causing the ...

`The chosen item is not appearing in view`

Having trouble displaying the skill label on the value.map function. I've tried every possible solution I can think of, including adding text inside the span but outside value.map. The text shows up, but the selected item does not. I even console log ...

Adjusting the full width TD media query for HTML emails on iPhone is not functioning properly

I'm encountering an issue with implementing a media query for an HTML email to display correctly on iPhones. Here is a screenshot showing the problem: Upon testing on Android, the footer aligns properly, but on iPhone it doesn't stack as intende ...

Navigate through the overlay div to access the canvas, with a delegated click event triggered by the coordinates

Currently, my canvas application does not have a built-in zoom and pan feature. To work around this limitation, I have created an overlay on top of the canvas with pointer-events: all and opacity: 0, allowing users to zoom and pan seamlessly. However, I ...

The webpage appears to be operating smoothly when tested locally and on JSFiddle, however it is encountering

Just joined and brand new to this game. Completed a course at work over the last few weeks, and I must say, I'm really enjoying it. However, when I tried to create a page using Bootstrap styling and added an accordion and a carousel, both features fai ...

Tips on creating CSS for an element with certain text within it?

I have a text element List that I need to select. Instead of using xpath like - //li[text()='List'] I want to use css. How can I write css for this? I attempted the following after referring to but it didn't work. li:contains('List& ...

Showcase three elements next to each other on the same row (Photo Gallery, Description, and Contact Form)

I am attempting to showcase three divs in a single line while working on a Subtheme for the Drupal 8 Bootstrap Theme. These particular divs are fields nested within a view known as viewproducts. The divs include: Juicebox Photo Gallery; ...

What is the best way to affix this image to the bottom of the div?

I need the smaller, grey image to stay at the bottom of its container (meadow image) even when resizing the window. I've tried using auto values on attributes but it seems to be related to positioning which I can't quite figure out. Any ideas? Th ...

Is it possible to vary the font size for different fonts within a single page?

Imagine using Helvetica for English text and a unique, exotic font (from an ASCII perspective) for another language on the same page. Even with the same font size, one font may appear larger to the eye. Is it possible to specify different font sizes for ...

How can the carousel item numbers be adjusted for proper display?

I'm currently working on an Angular app and have encountered a task related to displaying 5 cards using mdbootstrap. I want to showcase these cards in a carousel format, but with a twist - I only want the carousel functionality to be active on mobile ...

Utilizing flexbox for dynamic width adjustment with flex-grow functionality

Currently, I am in the process of configuring a menu bar using this particular template I have been attempting to achieve this layout utilizing FlexBox, but it appears that there is an issue. Here is the HTML code: <nav> <ul> < ...

Novice level: transforming a numerical list with a non-string input

Currently working on creating a simple tic tac toe game in Python, I have hit a roadblock in modifying the list used to update the grid: grid = [" ", "1", "2", "3", "4", "5", "6", " ...

Is it usual for the container to overflow with div content?

Is this common CSS behavior? How can we make the container wrap around the button? http://jsfiddle.net/afrontrow/yepw7oLw/ CSS: .button { background: grey; padding: 10px 20px; } .container { border: 1px solid black; } HTML: <div class= ...

Leverage JavaScript to display images based on the class of the element

I am looking to display specific logos based on a user's search for a product within a certain category. Initially, the logo image element is hidden (display none) and will be assigned a class corresponding to the category ID. The category ID will det ...