How can I disable a select element in Laravel 5?

Hey everyone!

Currently using Laravel 5 and trying to style the "select" class as "selectpicker".

I'm facing an issue where I want to disable or hide the selected option when clicked, as I'm creating a div with the option's content right below it. If the select option is not disabled or hidden, it keeps creating a new div.

I've been struggling with this for the past couple of days...

<select id="interlocutorsList" name="interlocutorsList" class="selectpicker" data-width="100%" data-style="btn-info select2" data-live-search="true">
 <option id="titleSelectInterlocutor" class="" value=""  selected>Make a choice</option>
  @foreach( $othersInterlocutors as $interlocutor)
   <option value="{{$interlocutor->id}}" id="option{{$interlocutor->id}}" class="" style="overflow: hidden; width:100%;" data-tokens="">{{$interlocutor->name.' '.$interlocutor->firstName}}
   </option>
  @endforeach
</select>
$("#interlocutorsList").change(function () {
 $('#titleSelectInterlocutor').addClass("d-none");
 var interlocutorSelect=$("#interlocutorsList").val();
 $('#option'+interlocutorSelect).addClass("d-none");
 var nameSel=interlocutorsTab[interlocutorSelect];
 $(".interlocutorSel").append("<div class='row rowSensor"+interlocutorSelect+"'><div class='col-lg-12 mb-1'><p class='d-inline' style='color: #000;'>"+nameSel+"</p><input id='interlocutor"+interlocutorSelect+"' type='hidden' name='interlocutor"+interlocutorSelect+"' value='on'><button type='button' id='btnSup" + interlocutorSelect + "' class='btn btn-outline-info buttonSubmit float-right'>-</button></div></div> ");

});

$(".interlocutorSel").on("click", "button[id^='btnSup']", function () {
 var idBtnClick = $(this).attr("id");
 var nbCustom = idBtnClick.substring(6);
 $('.rowSensor' + nbCustom).remove();
 $('#option'+nbCustom).removeClass("d-none"); 
});

Answer №1

To disable an option during an event, you can simply add `disabled='disabled'` within the option tag.

<option value="opel" disabled='disabled'>Opel</option>

If you are using jQuery versions older than 1.6, you can do the following:

jQuery('#option'+nbCustom).attr("disabled", 'disabled');

If you are using jQuery 1.6 or newer:

jQuery('#option'+nbCustom).prop("disabled", true);

See the example below:

<!DOCTYPE html>
<html>
<body>

<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel" disabled='disabled'>Opel</option>
  <option value="audi">Audi</option>
</select>
  
</body>
</html>

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

A PHP script or regular expression that swaps the positions of characters within a string

(Update1: per suggestion by George Cummins) I need assistance in creating a regex search and replace or a php function to switch positions of characters within a string. (since I couldn't figure it out on my own) For example, if I have the following C ...

Steps for launching Angular 5 application using Node.js server

I have developed an Angular 5 application that retrieves data from a node.js server. I successfully deployed the application to my web server hosted by FastComet, which supports node.js, but unfortunately, the server does not seem to be functioning properl ...

Babel is failing to transpile the Modal component from material-ui-next to ES5

Issue with Babel transpiling Modal component from material-ui-next Here is my .babelrc configuration: { "presets": ["es2015", "react", "stage-1", "stage-2", "stage-3"] } This is the webpack-config.js setup: var webpack = require('webpack'); ...

Angular/Ionic - How can I prevent my function from being called repeatedly while typing in an input field?

I am currently in the process of self-teaching how to construct an Angular/Ionic application. To store JSON, I am utilizing Backand and aiming to retrieve a random JSON value each time the page is refreshed. An issue I am facing is that the random call fu ...

Incorrect sorting of tables in Bootstrap

I've been struggling to position 3 tables as shown in the image, but they keep stacking vertically instead of horizontally. The desired layout is to have two tables on one side and three on the other side. Can anyone provide guidance on achieving this ...

Obtain an array of responses using jQuery ajax and pass it to PHP

I'm working on a project where I need to populate a table from MySQL based on a selection made in a select box using jQuery AJAX. Currently, this is the jQuery code that I have written. I am able to display the result in an alert box, but I am uncerta ...

The measure of the leaflet map's vertical dimension in a Shiny module application

I'm facing an issue while trying to incorporate my small app as a module within my larger app. Everything seems to be working fine except for the height of the leaflet map. In the standalone app, I had: ui <- fluidPage( tags$style(type = "te ...

The functionality of jQuery .load() is not fully compatible with Drupal 7 Views

When using .load() for ajax loading pages from the menus, everything on the view is loaded without taking paging into account. For example, if there are 100 items, it will display all of them at once with paging at the bottom. Clicking on any of the view& ...

Altering the vertical position of <li> within the nav bar without impacting the size of the nav bar is proving to be a

How can I adjust the vertical position of li elements without impacting the height of the navigation bar? Below is the code snippet in question: body { margin: 0px; } ul { margin: 0px; padding: 0px; list-style: none; padding-left: 5px; pad ...

Having trouble retrieving the attribute of an appended element in jQuery?

I am facing an issue where I am unable to retrieve the ID-attribute of an element that has been appended into my HTML. Each time I try, the result is always 'undefined'. How can I resolve this problem? jQuery('form#formular').append(&a ...

angularjs controller cross-validation for forms

I am faced with a dilemma involving two controllers: btnController and formController. Specifically, I need to submit a Form from another controller action while ensuring that the function in the controller is only called when the Form Validation is Valid. ...

What is the best way to set the width of an element to 100% while accounting for padding

My dilemma involves an HTML input field. The input currently has padding: 5px 10px;, but I need it to occupy 100% of its parent div's width (which is fluid). If I try using width: 100%;, the input ends up being wider than intended due to the padding ...

Tips for successfully implementing wp_ajax and jQuery $.post together

Referencing directly from the WP Codex: http://codex.wordpress.org/AJAX_in_Plugins Including this code snippet in my functions.php file: add_action( 'admin_footer', 'my_action_javascript' ); function my_action_javascript() { ?> &l ...

Scrolling to an id within dynamically loaded content using jQuery after an ajax request

After loading content via ajax, I am unable to scroll to the item, as the page remains at the top. Strangely, when I refresh the page dynamically with content caching enabled, the page scrolls to the desired target. var startPageLoader = function( pid, si ...

Utilizing JavaScript to manage a div's actions throughout various pages

I am currently working on an HTML project that involves having a collapsible text box on each page. However, I am struggling to figure out the best way to achieve the desired behavior. Essentially, some of the links will belong to a class that will set a v ...

The database query did not find any results for 3D secure payments

I have successfully implemented a Stripe integration that utilizes Payment Intents and Stripe Elements' Payment Element, following the guidelines in the Quickstart guide provided by the documentation. Additionally, I have configured webhooks using Spa ...

Is it possible to capture "global" events while handling nested iframes?

My dilemma involves capturing a keypress event, but the user can potentially be navigating through multiple layers of iframes nested within each other. Initially, I attempted to add the event listener to the document, only to realize that it wouldn't ...

Looking to adjust the alignment in BootsFaces? Want to move the final link of the navbar to the right corner?

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.c ...

TypeScript raises an issue with a Vue component property that has been defined using vue-property-decorator

I have a Vue component with a property defined using a decorator: import { Component, Vue } from "vue-property-decorator" @Component({ props: { myId: String, }, }) class TestProp extends Vue { myFuncti ...

Selenium Webdriver faced challenges in identifying dynamically created scripts using CSS selectors

I am in need of assistance with the following: Requirement: On the homepage, there is a navigation menu bar that changes appearance when scrolling down. This change is reflected in the body class name switching from "home" to "home scriptable persistent-o ...