2

I have the following bootstrap markup and jQuery script and my select list item is not being pre-selected:

    <!-- Text input-->
    <div class="form-group">
      <label class="col-md-4 control-label" for="serviceType">Service Type</label>
      <div class="col-md-1">
        <select id="serviceType" name="serviceType" class="form-control">
          <option value="S">S</option>
          <option value="T">T</option>
          <option value="X">X</option>
        </select>
        <script>
          //$('[name=serviceType]').val( T ); // not working
          //$('#serviceType').val( T ); // not working
          //$('[name=serviceType]').val( "T" ); // not working
          $('#serviceType').val( "T" ); // not working
        </script>
      </div>
    </div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

Can anyone see what is wrong with the jQuery? Thanks.

Appended Note #1: My jQuery CDN lib link is right before the closing tag whereas this container and javascript is up higher in the web doc. Does that matter?

Appended Note #2: Added the jQuery CDN lib for complete view.

1
  • I have created a demo for you jsfiddle.net/9gjjj1tm. Please check. Commented Nov 6, 2015 at 0:50

3 Answers 3

2

can you try this

$(document).ready(function () {

$('select option[value="T"]').attr("selected",true); });

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

5 Comments

does not work in my web doc @Francis. does this <script> container need to come after the jQuery lib in the document flow?
Hi H.Ferrence, your code is working properly I've tested it. import first the jquery lib before anything else.
"import first the jquery lib before anything else" -- meaning the jQuery CDN lib must be placed above my script in order for it to work? Placement and order of the lib and my script matter?
Yep, once I move my script after the jquery lib it works. so placement and order matter.
Ok, learning something because I always thought $(document).ready(function () {...}); meant I could position the the CDN lib at the bottom of the doc.
1

You need to call this code on page load. You should be using $( '#serviceType' ).val('T')

Please check the code below

  <!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="serviceType">Service Type</label>
  <div class="col-md-1">
    <select id="serviceType" name="serviceType" class="form-control">
      <option value="S">S</option>
      <option value="T">T</option>
      <option value="X">X</option>
    </select>
    <script>
     $(function() {
      $( '#serviceType' ).val('T'); 
        });
    </script>
  </div>
</div>

Dont forget to include jQuery file.

2 Comments

thanks @prashanth. I'll test it out. My jQuery is present but at the bottom of the document - after this section. Is that the reason?
does not work in my web doc @prashanth. does this <script> container need to come after the jQuery lib in the document flow?
0
$('#serviceType').val('T');

works

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.