Default Select Option Based on Input Value

Kurai Bankusu

I have the following select on my form

<select id="select-1">
    <option value="11">Option 11</option>
    <option value="12">Option 12</option>
    <option value="13">Option 13</option>
</select>

And a hidden input

<input id="hidden-input-1" value="13">

When my page loads, hidden-input-1 could have a value of either 11, 12, or 13. I'd like to default my select to whatever the value of this input might be. So far I've tried to set this using .val():

$( document ).ready(function() {
    /*...
         do AJAX stuff to set input value
    ...*/
    $("#select-1").val($("#hidden-input-1").val());
});

Which just returns a blank selection. Any ideas on how to nail this?

J Santosh

You are trying to access the elements when they are not loaded into DOM, so you are not getting any value, Below the correct way to do.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="select-1">
  <option value="11">Option 11</option>
  <option value="12">Option 12</option>
  <option value="13">Option 13</option>
</select>
<input id="hidden-input-1" value="13">
<script>
  $(document).ready(function() {
    $("#select-1").val($("#hidden-input-1").val());
  });
</script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

CakePHP2 - Default value for input - select with option multiple

From Dev

Select option box based on value of an input box in appended selectbox

From Dev

How to select an option based on value

From Dev

Select option default based on database variable

From Dev

Dynamically set the VALUE of an text-INPUT element to default by clicking SELECT option

From Dev

Dynamically set the VALUE of an text-INPUT element to default by clicking SELECT option

From Dev

JS: Change input type based on select option?

From Dev

select option based on all values in an input

From Dev

Set default value of select to null option

From Dev

Select Option Value return Integer by default

From Dev

Codeigniter in select option default value not working

From Dev

how to set a default value for select option

From Dev

Select Option Value return Integer by default

From Dev

Skip select option if default value come

From Dev

The default value is not ? object:null ? in select option Angularjs

From Dev

Default value to select option menu is not recognized

From Dev

filter based on SELECT and input value

From Dev

JavaScript - How to change input text value based on Gender Selection in Select Option

From Dev

jQuery Show / Hide Option Based on Input Value

From Dev

Match an input text with a value of an option and auto select it

From Dev

Update input value depending on select option

From Dev

Change value of an input field by changing the select option

From Dev

Feed select option value with textbox/hidden input?

From Dev

Change input value based on select input

From Dev

jQuery - remove select option based on text not value

From Dev

Select Dropdown option based on its text not value

From Dev

jquery - Set select option based on json value

From Dev

Change value of select option based on checkbox

From Dev

change ajax url based on value of select option

Related Related

  1. 1

    CakePHP2 - Default value for input - select with option multiple

  2. 2

    Select option box based on value of an input box in appended selectbox

  3. 3

    How to select an option based on value

  4. 4

    Select option default based on database variable

  5. 5

    Dynamically set the VALUE of an text-INPUT element to default by clicking SELECT option

  6. 6

    Dynamically set the VALUE of an text-INPUT element to default by clicking SELECT option

  7. 7

    JS: Change input type based on select option?

  8. 8

    select option based on all values in an input

  9. 9

    Set default value of select to null option

  10. 10

    Select Option Value return Integer by default

  11. 11

    Codeigniter in select option default value not working

  12. 12

    how to set a default value for select option

  13. 13

    Select Option Value return Integer by default

  14. 14

    Skip select option if default value come

  15. 15

    The default value is not ? object:null ? in select option Angularjs

  16. 16

    Default value to select option menu is not recognized

  17. 17

    filter based on SELECT and input value

  18. 18

    JavaScript - How to change input text value based on Gender Selection in Select Option

  19. 19

    jQuery Show / Hide Option Based on Input Value

  20. 20

    Match an input text with a value of an option and auto select it

  21. 21

    Update input value depending on select option

  22. 22

    Change value of an input field by changing the select option

  23. 23

    Feed select option value with textbox/hidden input?

  24. 24

    Change input value based on select input

  25. 25

    jQuery - remove select option based on text not value

  26. 26

    Select Dropdown option based on its text not value

  27. 27

    jquery - Set select option based on json value

  28. 28

    Change value of select option based on checkbox

  29. 29

    change ajax url based on value of select option

HotTag

Archive