3

i founded more example about this, but when i try , it's not work.

$(window).load(function () {
$("#country").html('Please select in here');
});
<select id="country">
<option value="None">-- Select --</option>
<option value="China">China</option>
<option value="United State">United State</option>
<option value="Malaysia">Malaysia</option>
</select>

Please see it and tell me "Why it's not working?".Thank guys

8
  • Uncaught ReferenceError: $ is not defined Commented Sep 17, 2015 at 3:01
  • Dear @Vohuman, I don't understand , please detail it , I want when load , text of select is : Please select in here such as $("#country").html('Please select in here');. Commented Sep 17, 2015 at 3:04
  • you did not include jquery script Commented Sep 17, 2015 at 3:06
  • Dear @Heyyou , not have a textbox ? Commented Sep 17, 2015 at 3:06
  • Do you want to replace the text content of the first option? Commented Sep 17, 2015 at 3:07

5 Answers 5

4

It's impractical to replace the whole content between <select id="country"> and </select> with 'Please select in here' ? If you just want to replace the first option's content, then try this.

$(window).load(function () {  
  //$("#country").html('Please select in here');
});

$(document).ready(function() {
  //$("#country").html('Please select in here');
  $("#country").find("option:first-child").html('Please select in here');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="country">
<option value="None">-- Select --</option>
<option value="China">China</option>
<option value="United State">United State</option>
<option value="Malaysia">Malaysia</option>
</select>

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

1 Comment

Dear @UKA , it's look well.
2

Not quite sure what you want, but if you want to add another option just append/prepend new option into select like so:

$(window).load(function () {
$("#country").prepend('<option value="" selected>Please select in here</option>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="country">
<option value="None">-- Select --</option>
<option value="China">China</option>
<option value="United State">United State</option>
<option value="Malaysia">Malaysia</option>
</select>

1 Comment

Yub , It's working for me , When load , we use prepend as @Norlihazmey Ghazali to say.
1

Please select in here:
<select id="country">
<option value="None">-- Select --</option>
<option value="China">China</option>
<option value="United State">United State</option>
<option value="Malaysia">Malaysia</option>
</select>

You can't add text in the select. You need to add in front of it.

If you really want to add the text after load, put one more div in front of the select to control

Comments

1

I am not entirely sure what you are trying to do, but if you just want to change the default text, you probably are looking for something like this:

<select id="country">
    <option value="None" selected>Please select in here</option> <!-- set the default selection -->
    <option value="China">China</option>
    <option value="United State">United State</option>
    <option value="Malaysia">Malaysia</option>
</select>

and then the JS to set a new value would be:

$(window).load(function () {
    $("#country").val('China');
});

Comments

1

Bit confuse what you trying to achieve

To Pre append

$('#country').prepend('<option>Please select in here</option>');

To dynamically Select:

$('#country').val("None")

Add new Option:

$('#country').append(new Option('Foo', true));

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.