最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Populating div in real time with form input data - Stack Overflow

programmeradmin2浏览0评论

I have a form (just one field) which I am populating with some data. I would like to get me Div container populated with the data I put there in a real time. So no refresh, after I type in some text in the form field, I get it in the div as well. Does anybody know how to do it? JQuery, AJAX?Any advice appreciated.

I have a form (just one field) which I am populating with some data. I would like to get me Div container populated with the data I put there in a real time. So no refresh, after I type in some text in the form field, I get it in the div as well. Does anybody know how to do it? JQuery, AJAX?Any advice appreciated.

Share Improve this question asked Jun 28, 2011 at 0:07 VonderVonder 4,05915 gold badges46 silver badges63 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5
var div = $('div')[0];

$('input').bind('keyup change', function() {
    div.innerHTML = this.value;
});

Try it out: http://jsfiddle/H6P2z/

Of course you should make the selectors specific to your actual elements.

With jQuery you can do this very easily:

$('#textFieldId').bind('change', function (event, previousText) {
    $('#divId').html($(this).val());
});

You could bind an onkeyup event to the input field. When the event fires, you can grab the contents of the field and do what you want with it.

This might look like this:

$(selector).keyup(function(e) {
   // find the value of the field
   var text = $(this).val();

   // do something with it
   $(yourDivSelector).html(text);
});
发布评论

评论列表(0)

  1. 暂无评论