Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display text onchage() of Combobox item in JavaScript

I have a form where I'm creating a combo dynamically in a javascript file. When the combo-box item changes, I want to display sort of a help text right below the combo.

I am creating the combo(with the id: WhereOperatorCombo)as shown below and have added an event listener.

$("#lot").append(
          "<h5> Select an Operator </h5>"+
          "<div class='col-md-12'>"+
            "<select id='WhereOperatorCombo' name='WhereOperatorCombo' class='form-control'>"+
            "<option value='Select an option'>Select an option</option>"+
            "<optgroup label='--Comparison Operators--'>"+
                "<option value='equal'>=</option>"+
                "<option value='notequal'>!=</option>"+
                "<option value='gt'>"+gt+"</option>"+
                "<option value='lt'>"+lt+"</option>"+
                "<option value='gte'>"+gte+"</option>"+
                "<option value='lte'>"+lte+"</option>"+
                "<option value='ngt'>"+ngt+"</option>"+
                "<option value='nlt'>"+nlt+"</option>"
    );

   $("#WhereOperatorCombo").append(
              "<optgroup><optgroup label='--Logical Operators--'>"
   );


   for (var k = 0; k < logicalOperatorListArray.length; k++)
   {
       $("#WhereOperatorCombo").append(
                  "<option value="+logicalOperatorListArray[k]+">"+logicalOperatorListArray[k]+"</option>"                              
       );
   }

   $("#WhereOperatorCombo").append(
              "</optgroup></select>" +
              "<div id='onchangeDefinition' class='col-md-12'></div>"+
              "</div>"                              
   );

   document.getElementById("WhereOperatorCombo").addEventListener("change", function() {
        displayValueinput();
    }, false);

displayValueinput() method

function displayValueinput()
{
    var myNode = document.getElementById("onchangeDefinition");
    var fc = myNode.firstChild;

    while( fc ) {
        myNode.removeChild( fc );
        fc = myNode.firstChild;
    }

    var choice=document.getElementById("WhereOperatorCombo");
    var selectedOperator = choice.options[choice.selectedIndex].text;

    if(selectedOperator == "=")
    {    
        $("#onchangeDefinition").append(
                  "<h5> This operator filters the records that are equal the value that you provide</h5>"                               
       );
    }

    else if(selectedOperator == "!=")
    {    
        $("#onchangeDefinition").append(
                  "<h5> This operator filters the records that are NOT equal the value that you provide</h5>"                               
       );
    }
...

But the issue that I am facing is that nothing is being displayed in the onchangeDefinition division.

I tried displaying an alert within the if(selectedOperator == "!=") decision just to check whether it goes through that block, and I am getting the alert. But the help text is not being appended to the division as I intended.

Any suggestions in this regard will be highly appreciated.

like image 229
Nayantara Jeyaraj Avatar asked Jan 02 '26 01:01

Nayantara Jeyaraj


1 Answers

Add onchangeDefinition div outside of select element.

$("#WhereOperatorCombo").closest('div').append()

Try below code as sample

$("#lot").append(
  "<h5> Select an Operator </h5>" +
  "<div class='col-md-12'>" +
  "<select id='WhereOperatorCombo' name='WhereOperatorCombo' class='form-control'>" +
  "<option value='Select an option'>Select an option</option>" +
  "<optgroup label='--Comparison Operators--'>" +
  "<option value='equal'>=</option>" +
  "<option value='notequal'>!=</option>" +
  "<option value='gt'>gt</option>" +
  "<option value='lt'>lt </option>" +
  "<option value='gte'>gte</option>" +
  "<option value='lte'>lte </option>" +
  "<option value='ngt'> ngt </option>" +
  "<option value='nlt'>nlt</option>"
);

$("#WhereOperatorCombo").append(
  "<optgroup><optgroup label='--Logical Operators--'>"
);
logicalOperatorListArray = [];

for (var k = 0; k < logicalOperatorListArray.length; k++) {
  $("#WhereOperatorCombo").append(
    "<option value=" + logicalOperatorListArray[k] + ">" + logicalOperatorListArray[k] + "</option>"
  );
}

$("#WhereOperatorCombo").closest('div').append(
  "</optgroup></select>" +
  "<div id='onchangeDefinition' class='col-md-12'></div>" +
  "</div>"
);

document.getElementById("WhereOperatorCombo").addEventListener("change", function() {
  displayValueinput();
}, false);


function displayValueinput() {

  var myNode = document.getElementById("onchangeDefinition");
  var fc = myNode.firstChild;

  while (fc) {
    myNode.removeChild(fc);
    fc = myNode.firstChild;
  }
  debugger;
  var choice = document.getElementById("WhereOperatorCombo");
  var selectedOperator = choice.options[choice.selectedIndex].text;

  if (selectedOperator == "=") {
    $("#onchangeDefinition").append(
      "<h5> This operator filters the records that are equal the value that you provide</h5>"
    );
  } else if (selectedOperator == "!=") {
    $("#onchangeDefinition").append(
      "<h5> This operator filters the records that are NOT equal the value that you provide</h5>"
    );
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<div id="lot"></div>
like image 97
Yogen Darji Avatar answered Jan 03 '26 15:01

Yogen Darji



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!