简体   繁体   中英

How to update span when text is entered in form text field with jquery

I would like a span to update when a value is entered into a text field using jquery. My form field has a text box with the name "userinput" and i have a span with the id "inputval". Any help would be greatly appreciated.

UPDATE: although you marked this as the correct answer, note that you should use the keyup event rather than the change event or the keydown

$(document).ready(function() {
    $('input[name=userinput]').keyup(function() {
      $('#inputval').text($(this).val());
    });
    });

Try the following, you have to call again keyup() to trigger for the last char:

$(".editable_input").keyup(function() { 
    var value = $(this).val();
    var test = $(this).parent().find(".editable_label");
    test.text(value);
}).keyup();

Try this. Be sure that you understand what is going on here.

// when the DOM is loaded:
$(document).ready(function() {
    // find the input element with name == 'userinput'
    // register an 'keydown' event handler
    $("input[name='userinput']").keydown(function() {
        // find the element with id == 'inputval'
        // fill it with text that matches the input elements value
        $('#inputval').text(this.value);
    }
}
$(function() {
    $("input[name=userinput]").keydown(
      function() {
        $('#inputval').text(this.value);
      }
     )
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM