0

I have a textbox and selected option list of 4 options like below...

<select id="select2" class="form-control">
                <option value="pre">Pre</option>
                <option value="rep" selected="selected">Rep</option>
                <option value="new">New</option>
                <option value="dec">Dec</option>
              </select>

and a textbox where user input some keywords and based on keywords i want to change selected option in above option list

<td id="fpv_col1"  contenteditable="">decorating and repair work</td>

main value for selction comes from php and ajax based on user input so i just want to set selected value in above option list

below is my jquery code

$(document).on('keyup', '#fpv_col1', function(event) {
    var desc_text = $(this).text();
    $.ajax({
            url: 'php/fpv_lbyl_prediction.php',
            type: 'POST',
            data:{
                    value1:desc_text,

            },
            dataType:'text',
            success: function(data){
                $("#error_msg").fadeIn();
                $("#error_msg").html("<strong>Success!</strong> "+data);
                //$('select#select2 option').removeAttr('selected');
                //$('select#select2').find('option[value='+data+']').attr("selected",true);

                $('select#select2 option').removeAttr('selected').filter('[value='+data+']').attr('selected', true);  
            }
        });

event.preventDefault();

});

my jquery code works fine, it set selected option but not displaying selected option, it saw using inspect

1
  • Are you using any plugin such as select2? have you tried $('#select2').val(data);? Commented Oct 20, 2016 at 9:09

1 Answer 1

1

$(document).ready(function() {
  $('#otherCategory').keyup(function() {
    if ($(this).val() == 0) {
      $('#category').val(1);
    } else if ($(this).val() > 0) {
      $('#category').val(2);
    } else {
      $('#category').val(0);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="category" name="category">
  <option value="0">Please select</option>
  <option value="1">Clear</option>
  <option value="2">UnClear</option>
</select>

<input type="text" id="otherCategory" name="otherCategory">

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.