-2

I am just trying to replace the value of the following input field:

<input id="farbe" style="background-image: url('images/colors/RAL_1001.png'), no-repeat;" value="Farbton wählen">

the needed Value I get out of my autocomplete list. The alert works fine (correct value), but I do not get the value of the input field replaced.

$(document).ready(function () {
        $('#farbe').on('autocompletechange change', function () {
            var wert = this.value;
            alert(wert);
            $('#farbe').attr("value",wert);
        }); 
    });

What am I doing wrong?

Jamiec
  • 133,658
  • 13
  • 134
  • 193
ccalbow
  • 47
  • 1
  • 1
  • 10
  • 2
    Possible duplicate of [Set CSS attribute in Javascript?](http://stackoverflow.com/questions/5195303/set-css-attribute-in-javascript) – thesecretmaster Aug 16 '16 at 15:31
  • It looks like you're trying to read the `value` of a field and then set that same value back to the same field. of course this does nothing visible - you're setting it to the same value that is already there. – Jamiec Sep 07 '16 at 11:19
  • 1
    I'm voting to close this question as off-topic because it's not constructive. – EJoshuaS - Stand with Ukraine Aug 28 '18 at 00:22

2 Answers2

4

Try It Once

$(document).ready(function(){
  $('#change').click(function(){
      $("input:text").val("Raamu");
    
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txt-field" value="Samudrala"/>
<button id="change">Chang Value</button>
Samudrala Ramu
  • 2,088
  • 1
  • 15
  • 36
2

Use .val() to set the value and not try to change the attribute with .attr().

$('#farbe').val(wert);

Inside a callback you can even use this instead of a selector:

$(this).val(wert);
eisbehr
  • 12,243
  • 7
  • 38
  • 63
  • I have tried this already, but it does not change the value.... it still stys the same – ccalbow Aug 16 '16 at 13:56
  • Then create a fiddle or something, reproducing the problem or show us an error message. Because this **IS** working and definetly the correct solution! @ccalbow – eisbehr Aug 16 '16 at 13:59
  • This is not that easy since i am retrieving the data for the autocomplete out of a database. There is no error message, the value of the input just stays the same... – ccalbow Aug 16 '16 at 14:12
  • I can't help you any further without more info, messages or code. `.val()` IS the correct way to set the value. If it is not working for you, you did it wrong or something is blocking. But there is no way to help you any further now ... Here is a prove the it is working in general: https://jsfiddle.net/g9sxac28/ @ccalbow – eisbehr Aug 16 '16 at 14:15
  • when I submit this field, it sends the correct value . Could it be that the Code Inspector and firebug is not capable of visualizing the changes? the input value is the value that is send via Post - correct? Thanks for helping anyways. Hope this works now - – ccalbow Aug 16 '16 at 14:28