0

I am having a popup and a foreach on my view , it has a dropdown too, on a button click I need to get the selected value of the drop-down,

Each time I click it returns the first drop down value I couldn't use $(this).val()

Is there any way. This is my code

$( ".ecg_followup_submit" ).click(function(e) {

   alert($( ".reviewoutcome_change option:selected" ).val());

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
  <div class="col-md-8">
    <div class="row">
      <div class="col-md-6">
        <label for="exampleInputPassword1">Review outcome:</label>                                                                
      </div>
      <div class="col-md-6">
        <select class="form-control reviewoutcome_change" name="review_outcome" id="declined_reason">
          <option value="select">Select</option>
          <option value="normal">Normal</option>
          <option value="minor" >Minor</option>
        </select>
      </div>
    </div>
  </div>
</div>
<div class="row" style="height:10px;"></div>
<div class="row">
  <div class="col-md-8">
    <div class="row">
      <div class="col-md-6">
    </div>
  </div>
</div>
<div class="row">
  <div class="col-md-9"></div>
</div>
<div class="modal-footer" style = "margin-top:15px;">
  <div class="row">
    <div class="col-md-6">
      <input type="submit" class="btn btn-primary ecg_followup_submit" name="Submit" style = "width:50%; height:35px; margin-right:150px;" />
    </div>
  </div>
</div>

I have removed the unwanted codes

Thank you

1
  • It seems like your code already works. There is one dropdown and its selected value is shown in the alert. Commented Jun 2, 2021 at 10:06

3 Answers 3

2

To get the value of 'selected' option you do not need to use :selected. you can get the selected value like any other input. $('your selector').val()

However, if you do not change your selection, the first option will be selected by default.

Here is an example inspired from your code.

jQuery(document).ready(function($){
  $('.btn').click(function(e){
    e.preventDefault();
    
    console.log($('.reviewoutcome_change').val());
  
  
  })

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="form-control reviewoutcome_change" name="review_outcome" id="declined_reason">
  <option value="select">Select</option>
  <option value="normal">Normal</option>
  <option value="minor">Minor</option>
</select>

<button class="btn">show selected</button>

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

2 Comments

no didnt work its on a pop up and on a loop
please give a snippet of your actual code, it will help to inspect the problem properly.
0

just change

$( ".ecg_followup_submit" ).click(function(e) {
    
    alert($( ".reviewoutcome_change option:selected" ).val());
   
});

to this

$( ".ecg_followup_submit" ).click(function(e) {
    
    alert($( '.reviewoutcome_change' ).val());
   
});

2 Comments

no didnt work its on a pop up and on a loop
full code pls :") cz i still not get what u want
0

Find one common ancestor. I assume that you have multiple rows then?

$(".ecg_followup_submit").click(function (e) {
    const row = $(this).closest(".row")
    const selectedOption = row.find(".reviewoutcome_change");
    alert(selectedOption.val());
});

2 Comments

it retuens undefined
if you use row.find(".reviewoutcome_change"); instead like @kurnia336 suggested?

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.