0

I have a dropdown here in my edit view ,I am maintaining the data in my database by "value"(1 and 0) but would like to show it in the dropdown as "Active" and "Deactive")I would like to indicate the selected option , but its not working : here is the view:

<div class="form-group col-md-4" onChange="myNewFunction(this);" >
               <label for="pwd">Status</label>
             <select  class="form-control" id="Status" placeholder="Status" name="Status"  >
              <!-- <option value="">Select Status</option>  -->
                 <option value="1" >Active</option>
                <option value="0" >Deactive</option>
             </select>
             <div class="alert-danger"><?php echo form_error('Status'); ?> </div> 
            </div>

And here is the javascript:

   var conceptName = $('#Status').find(":selected").text();
    //alert(conceptName);  
    var el = document.getElementById('Status');
    var text = el.options[el.selectedIndex].innerHTML;
     ($.trim($("#Status").children("option:selected").text()));

alert(conceptName); => results: the first option of select always

currently I am able to get the innerHTML, but not selected option.

Roxana Slj
  • 311
  • 1
  • 5
  • 23

2 Answers2

1

I can't make comments yet but if your edit form is populated from PHP you can do something like this:

<option value="1"<?php if ($status == 1) echo " selected";?>>Active</option>
<option value="0"<?php if ($status == 0) echo " selected";?>>Deactive</option>

Replace $status with the variable that contains the status value from your DB.

mikeroq
  • 252
  • 2
  • 7
0

From

var conceptName = $('#Status').find(":selected").text();  

To

var conceptName = $('#Status option:selected').text();  

Try it, will surely work.

Rajen Trivedi
  • 1,225
  • 2
  • 5
  • 10
  • still not working and ,alert(conceptName); just give the first option always, I am maintaining the data by value 1and 0, does that cause the problem? – Roxana Slj Nov 20 '20 at 17:08
  • look at this fiddle..https://jsfiddle.net/rajtri/xybethn2/8/ – Rajen Trivedi Nov 20 '20 at 17:30
  • Dont need onchange(), this is a edit form , so basically I am getting the data from database which are (1 and 0), and just based on that want to show the selected option on the dropdown – Roxana Slj Nov 20 '20 at 17:41
  • in this scenario you should use if else condition in html file, your html code is not look like edit purpose code at first sight. – Rajen Trivedi Nov 21 '20 at 05:06