change div text with jquery

1

I have an 'input' and a 'div':

<input  type='text' id='valor' name='valor'>

<div id='valor'><b>R$ 1,00</b></div>

Well I need to change the value of 1.00 to the value that I type in the input. How can I do this in losing the css of the div value?

I wanted to do this using Jquery.

    
asked by anonymous 30.11.2016 / 10:43

1 answer

1

Well, first of all, you can not have two elements with the same ID on the page, not good practice. Change your input ID to something other than valorInput , or use a class.

You can use the jQuery change event for it like this:

$(document).ready(function() {
    $('#valorInput').on('change', function() {
    var value = $(this).val();
    $('#valor b').text('R$ '+value);
  });
});

If you have a structure something like this:

<input  type='text' id='valorInput' name='valor'>

<div id='valor'><b>R$ 1,00</b></div>

Fiddle: link

    
30.11.2016 / 10:52