-1

Hello I need help this my code and it always goes to else condition I want to comapre a text from dropdown if the value and text matches then I want to hide some div's :

jQuery("#myid").on('change', function () {
    if(jQuery('#myid').val() == 'Well, this is a dummy text') {
        alert(" hello");
    } else {
        alert("World");
    }
});



<select name="testtype" id="myid" class="validate[required]" style="font-size:12px; font-family:arial">
                <option selected="selected" value="">-- Make your selection --</option>
                <option id="first" value="Health, Safety and Environment Test for Operatives ">Health, Safety and Environment Test for Operatives </option>
                <option value="Supervisory-English only">Supervisory-English only</option>
                <option value="Health, Safety and Environment Test for Managers and Professionals -English only">Health, Safety and Environment Test for Managers and Professionals -English only</option>
                <option value="Demolition-English only">Demolition-English only</option>
                <option value="Highway Works-English only">Highway Works-English only</option>
                <option value="Specialist Work at Hight-English only">Specialist Work at Hight-English only</option>
                <option value="Lifts and Escalators-English only">Lifts and Escalators-English only</option>
                <option value="Tunnelling-English only">Tunnelling-English only</option>
                <option value="Heating, Ventilation Air Conditioning and Refrigeration (HAVCR)-English only">Heating, Ventilation Air Conditioning and Refrigeration (HAVCR)-English only</option>
                <option value="Plumbing (JIB)-English only">Plumbing (JIB)-English only</option>
            </select>
5
  • 1
    How do you expect anybody to help with that? Only you can know what jQuery('#myid').val() is and thus why the condition fails. Commented Dec 18, 2017 at 12:39
  • Show us Html code too ! Commented Dec 18, 2017 at 12:39
  • what element belong to #myid Commented Dec 18, 2017 at 12:40
  • select id is #myid Commented Dec 18, 2017 at 12:42
  • share html for element #myid ? Commented Dec 18, 2017 at 12:44

2 Answers 2

1

I want to comapre a text from dropdown

It looks like you want to match the text of the selected option not the value, then you should get the text using .text() instead of .val() that will return the value, the selector should be like :

jQuery('#myid option:selected').text()

Code:

jQuery("#myid").on('change', function() {
  //if (jQuery('#myid option:selected').text() == ...){
  //Or
  if (jQuery(this).val() == 'Health, Safety and Environment Test for Operatives ') {
    alert(" hello");
  } else {
    alert("World");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select name="testtype" id="myid" class="validate[required]" style="font-size:12px; font-family:arial">
  <option selected="selected" value="">-- Make your selection --</option>
  <option id="first" value="Health, Safety and Environment Test for Operatives ">Health, Safety and Environment Test for Operatives </option>
  <option value="Supervisory-English only">Supervisory-English only</option>
  <option value="Health, Safety and Environment Test for Managers and Professionals -English only">Health, Safety and Environment Test for Managers and Professionals -English only</option>
  <option value="Demolition-English only">Demolition-English only</option>
  <option value="Highway Works-English only">Highway Works-English only</option>
  <option value="Specialist Work at Hight-English only">Specialist Work at Hight-English only</option>
  <option value="Lifts and Escalators-English only">Lifts and Escalators-English only</option>
  <option value="Tunnelling-English only">Tunnelling-English only</option>
  <option value="Heating, Ventilation Air Conditioning and Refrigeration (HAVCR)-English only">Heating, Ventilation Air Conditioning and Refrigeration (HAVCR)-English only</option>
  <option value="Plumbing (JIB)-English only">Plumbing (JIB)-English only</option>
</select>

Sign up to request clarification or add additional context in comments.

3 Comments

Well, this is a dummy text this is text and value too it doesn't alert the hello
Ok please show us your version of the dropdown HTML code.
see question and I want to display hello on <option id="first" value="Health, Safety and Environment Test for Operatives ">Health, Safety and Environment Test for Operatives </option>
0

the mistake is you have assigned value of options to number & you are comparing with it so it returns false on if statement. but

if you compare with .text() it gets all text from every option that's why it fails in if statement

$("#myid").change(function(){

  if( $(this).val()=='Well,this is a dummy text' ) {
    alert('hello');
  }
  else
    alert("wolrd");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="myid">
<option value="1">wolrd</option>
<option value="2">hello</option>
<option value="Well,this is a dummy text">Well,this is a dummy text</option>

</select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.