change text of div with jquery

Asked

Viewed 13,921 times

1

Guys I have an input and a div':

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

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

Good need to exchange the value of 1.00 for the value 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.

1 answer

1


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

You can use jQuery’s change event for this:

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

If Voce has a structure something like this:

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

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

Fiddle: https://jsfiddle.net/qzqkmjah/

  • 4

    Making an observation in the @Khaosdoctor response, the event change jQuery, is only executed when it loses focus of the input, if you want to run the function while the user type, there are the events keyup, keydown and keypress, depends on what action you need. To learn more about the events: jQuery Events.

  • very obligatory to the 2 by the help :) gave right here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.