0

I have a drop down

  <div class="form-group">
 <select class="form-control" required name="uname" id="uname">
 <option value=""/>Select Your Name</option>
  <?php foreach ($this->getallusers as $users): ?>
 <option value="<?php echo $users['adminID'] ?>"<?= $users['adminID'] == $stories['Tm_id'] ? ' selected="selected"' : ''; ?>/><?php echo $users['UserName'] ?></option>
 <?php endforeach; ?>
</select>

Drop down have values.then when i am tried to get the selected drop down value from jquery.

var op=$('#uname option :selected').text(); 
 var opid=$('#uname option :selected').val(); alert(opid);

When i alert op(text) i always got Select Your Name and opid is always blank.but drop down i have values pakka.Any help would be Appreciated .

2
  • 1
    note, you are closing your option opening tag -> <option value=".." />...</option>, as you have /> between <?= $users['adminID'] == $stories['Tm_id'] ? ' selected="selected"' : ''; ?> and <?php echo $users['UserName'] ?> Commented Jan 10, 2019 at 6:25
  • @Sean can you please text it as answer Commented Jan 10, 2019 at 6:38

3 Answers 3

1

try to use just this code:

alert($("#uname").val());

////////

.val() - gets a value of any input and dropdown.

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

Comments

0

Try below code to get the currently selected value and text when select box is change.

$(document).ready(function(){
    $("select#uname").change(function(){
        var op = $(this).children("option:selected").text();
        var opid = $(this).children("option:selected").val();
        alert("You have selected - " + op + " -> " opid);
    });
});

Comments

0

Try removing the space from option and :selected on your jquery. So intead of

var op=$('#uname option :selected').text(); 
var opid=$('#uname option :selected').val(); alert(opid);

it should be

var op=$('#uname option:selected').text(); 
var opid=$('#uname option:selected').val(); alert(opid);

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.