JavaScript Unable to set value of input tag from dropdown list unable to restore input value state

I'm trying to set an input value from a dropdown list but it doesn't seem to work for me. this is my code:

<form>
    <select name="color" id="color">
        <option selected value="0">white</option>  
        <option value="1">orange</option> 
        <option value="1">black</option> 
        <option value="2">yellow</option>
        <option value="2">green</option>    
        <option value="4">gray</option> 
        <option value="4">red</option> 
        <option value="8">pink</option> 
        <option value="8">blue</option> 
    </select>
    <input type="text" id="output">
</form>
$('#color').bind('change', function() {
    $('#output').html($(this).find(':selected').html() + '<br>');
    console.log($(this).find(':selected').html());
});

It works when I replace <input type="text" id="output"> with <div id="output"></div>. Any solutions?

Answer:1

The #output element is an input, so you need to use val(), not html(), to set its value.

Also note that bind() is deprecated. You should use change() or on('change') instead. Try this:

$('#color').on('change', function() {
  $('#output').val($(this).find(':selected').text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <select name="color" id="color">
    <option selected value="0">white</option>  
    <option value="1">orange</option> 
    <option value="1">black</option> 
    <option value="2">yellow</option>
    <option value="2">green</option>    
    <option value="4">gray</option> 
    <option value="4">red</option> 
    <option value="8">pink</option> 
    <option value="8">blue</option> 
  </select>
  <input type="text" id="output">
</form>
Answer:2

Try this example,

$('#color').bind('change', function() {
    $('#output').val($('#color option:selected').text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
    <select name="color" id="color">
        <option selected value="0">white</option>  
        <option value="1">orange</option> 
        <option value="1">black</option> 
        <option value="2">yellow</option>
        <option value="2">green</option>    
        <option value="4">gray</option> 
        <option value="4">red</option> 
        <option value="8">pink</option> 
        <option value="8">blue</option> 
    </select>
    <input type="text" id="output">
</form>
Answer:3

Try this:

$(document).ready(function() {
  $('#color').on('change', function() {
    //alert( this.value );
     $('#output').val(this.value);
  });
});
Answer:4
<script type="text/javascript">
$('#color').bind('change', function(){
    $('#output').val($(this).find(':selected').html() + '<br>');
    console.log($(this).find(':selected').html());
});
Answer:5

Input tags always has value so you have to change code to below.

<script type="text/javascript">
$(document).on('change','#color', function(){

    $('#output').val($(this).find(':selected').html() + '<br>');

});

</script>
Answer:6

I am going to use SingalR with angular; my requirement is that just only one time gets a connection id as a singleton pattern and used all over a single app because in my case browser will load first ...

I am going to use SingalR with angular; my requirement is that just only one time gets a connection id as a singleton pattern and used all over a single app because in my case browser will load first ...

  1. signalr with multiple servers
  2. signalr client with multiple connections
  3. signalr multiple hubs
  4. signalr multiple connections
  5. signalr multiple hub instances
  6. signalr multiple tabs
  7. signalr multiple chat rooms
  8. signalr multiple hubs one connection
  9. signalr multiple hubs on same page
  10. signalr multiple connections to same hub
  11. signalr multiple instances
  12. signalr multiple clients
  13. signalr multiple hubs example
  14. signalr multiple groups
  15. signalr multiple browser tabs
  16. signalr multiple hub connections
  17. signalr multiple worker processes
  18. signalr multiple parameters
  19. signalr multiple rooms
  20. signalr multiple applications

I'm new to javascript and SVG and I have no graphical programming background and this is my first project using all of those. So I started to make a custom element just like Mike Goodwin answer ...

I'm new to javascript and SVG and I have no graphical programming background and this is my first project using all of those. So I started to make a custom element just like Mike Goodwin answer ...

I want to make use of one html tag into my javascript but don't know how to make use of that. For ex: <p>An absolute URL: <a href="https://www.w3schools.com">W3Schools</a></p>...

I want to make use of one html tag into my javascript but don't know how to make use of that. For ex: <p>An absolute URL: <a href="https://www.w3schools.com">W3Schools</a></p>...

  1. html into javascript variable
  2. insert html into javascript
  3. import html into javascript
  4. html input into javascript variable
  5. convert html into javascript
  6. add html into javascript
  7. convert html into javascript string
  8. insert html into javascript variable
  9. embed html into javascript
  10. html into string javascript
  11. inject html into javascript
  12. turn html into javascript string
  13. html form into javascript variable
  14. turn html into javascript
  15. write html into javascript
  16. html code into javascript
  17. load html into javascript variable
  18. include html into javascript
  19. load html into javascript
  20. put html into javascript

I have developed and packaged an offline Cordova-based HTML5 App for Android. It works fine, and some of the resources are accessed locally without an internet connection while other require an ...

I have developed and packaged an offline Cordova-based HTML5 App for Android. It works fine, and some of the resources are accessed locally without an internet connection while other require an ...