1

I have a radio button which when clicked is displaying select box below so I could choose an option. My problem is that I can't get the value from select box. It returns as null. I guess problem is that it should have another change event in it. Can you tell me how to put another change event inside this change event?

Select box:

<div class="row">
   <div class="col-md-6">
     <div class="radio clip-radio radio-primary">
          <input type="radio" id="radio3" name="vertical" value="Pricelist" />
            <label for="radio3">
            Pricelist
            </label>
     </div>
     <div id="Pricelist" class="desc">
         <div class="form-group">
           <select class="cs-select cs-skin-elastic" name="pricelist" id="pricelist_select">
              <option value="" disabled selected>Select pricelist</option>
              <option value="1">1</option>
              <option value="2">2</option>
           </select>
     </div>
  </div>

jQuery

//Displaying select box when type radio button is clicked
            $(document).ready(function() {
                $("input[name=vertical]").on( "change", function() {
                     var test = $(this).val();
                     $(".desc").hide();
                     $("#"+test).show();
                     $("#pricelist_select").val();
                     $('#pricelist_select').trigger("change");
                    alert($("#pricelist_select").find(":selected").val());
                    $.cookie('pricelist_select', $("#pricelist_select").val());
                } );

            });

CSS for hiding select box

.desc { display: none; }
3
  • Please click the <> button, insert the RENDERED HTML of the php since this is NOT a PHP question. Then click tidy and you have a minimal reproducible example. Also you need to include the cookie JS Commented Dec 25, 2016 at 18:55
  • You should not get any value from a disabled select. You are not posting enough of the code that gives you the problem - here is how to get the text in a simpler way: stackoverflow.com/questions/1643227/… Commented Dec 26, 2016 at 9:31
  • I have edited my qestion, so maybe now it's more clear what is my problem. Commented Dec 26, 2016 at 16:43

1 Answer 1

1

Just use $(this).val() instead or $('#pricelist_select').val() if you are not inside the scope of the #pricelist_select element:

$('#pricelist_select').change(function() {
  console.log($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="cs-select cs-skin-elastic" name="pricelist" id="pricelist_select"> 
  <option value="" disabled>Select pricelist</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

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

18 Comments

I like you're solution and I have tried it, but I always get the last value. I don't understand why? How can I debug that?
Any chance you have 2 select elements? Otherwise it makes no sense at all (if you use the exact solution I gave).
Yes, I have three of them on same page. I thought id is refering to exact select.
Yes. My next question is if they each have unique id. Can you create a working example in jsfiddle.net?
From what I see everything works great in your jsfiddle. what is the problem?
|

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.