check dropdown value contain specific text using jquery

jquery check if specific option is selected
jquery get selected option text onchange
jquery select option by text
jquery contains
jquery get selected option value
jquery select dropdown
queryselector contains(text)
jquery text search

In a set of dropdown menus, if selected value contain text Misc, I would run specific code. I am using below code to check the value of dropdown, but not working.

$(document).ready(function(){

        $("[id^='select_debit']").on('change', function() {


             if ( this.value ~= 'Misc')

                {

                    run code A

                } else {

                    run code B
                }
        });
});

Any suggestions to achieve this ?

EDIT:

To be more specific, dropdown does not have specific value called Misc, but with some additional text ex: Travel Misc, Web Misc.

So I am looking for any options which has word "Misc" in it

You can use

var n = (this.value).includes("Misc");

To check if string contains in given string

You can get more details about includes from here.

$(document).ready(function() {

  $("[id^='select_debit']").on('change', function() {


    if (this.value.includes('Misc'))

    {

      console.log('A');

    } else {

      console.log('B')
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="select_debit">
  <option>Select option</option>
  <option value="Travel Misc">Travel Misc</option>
  <option value="Web Misc">Web Misc</option>
  <option value="Other">Other</option>

</select>

:contains() Selector, Description: Select all elements that contain the specified text. As with attribute value selectors, text inside the parentheses of :contains() can be written as a� check if dropdownlist contains value c#, check if select option is empty javascript, check jquery contains class, javascript check if option exists in select, javascript check if select option is selected, jquery check if dropdown contains text, jquery check if specific option is selected, jquery contains class, jquery find class, jquery remove class, remove class jquery.

How do I get the text value of a selected option?, text value of a selected option? Select elements typically have two values that you want to access. How do I check/uncheck a checkbox input or radio button? How do I replace text from the 3rd element of a list of 10 items? $( "#selectOption" ).click(function() { $("#myDropdown option:contains(Option 2)").attr('selected', 'selected'); }); Now let’s check it. When we run our code on browser then if we click the button, we will see that the option with text “Option 2” with the value “2” selected. Simple Animation Example Using jQuery. Check whether a checkbox is checked in jQuery

$(document).ready(function()
{
   $("[id^='select_debit']").on('change', function() 
   {
      if ($(this).val() == 'Misc')
      {
         run code A
      } else {
         run code B
        }
   });
});

How to get text of specific option tag using jQuery?, It contains well written, well thought and well explained computer science and programming articles, In order to select the text of a selected option or any specific option, jQuery allows doing that easily Example: This example selects the text of the specific option(GFG_2) tag. Check out this Author's contributed articles. How to Get Text of Selected Dropdown Option Using jQuery. To perform this task, you have to use the $(this) selector of jQuery. The $(this) selector selects the currently selected value of the drop-down. However, you also have to use the find() of jQuery to find the currently selected option. If you do not use the find function of jQuery, the $(this) selector gives all the option text including the selected one. So, use the find() of jQuery for getting the option text.

How to get selected text from a drop-down list using jQuery , It contains well written, well thought and well explained computer science and We can select text or we can also find the position of a text in a drop down list The val() method is an inbuilt method in jQuery which is used to return or set the value of attributes for the selected Check out this Author's contributed articles. which is basically checking to see if an attribute contains a value. In short is the same as what you were doing, but the ":contains" looks at text [hopefully now you now what qualifies as text :)] where as the [attribute*=value] searches for the "text" in the attribute's value.

Select option from dropdown by text in jQuery, the jQuery tag. Here in this example, we are going to select option contains a particular text by clicking a button. Select option from dropdown select tag by text instead of value in jQuery Check whether a checkbox is checked in jQuery. How to change selected value of a drop-down list using jQuery? With jQuery, it is easy to writing one line of code to change the selected value from a drop-down list. Suppose, you have a select element and you need to select one of its options based on one of its values.

jQuery :contains() Selector, jQuery :contains() Selector The :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above). See the Pen jquery-practical-exercise-31 by w3resource (@w3resource) on CodePen. Contribute your code and comments through Disqus. Previous: Change button text using jQuery.

Comments
  • Simply it should be $(this).val() not this.value.
  • Also it's != for not equal. So it's if ($(this).val() != 'Misc')
  • $('option:selected' , this).text().indexOf('Misc') > -1
  • In the context of the code. this would have a different meaning.
  • Its working, May I know problem, because I am not getting