开发者

Whats wrong this live preview jQuery script?

i have this script in jsfiddle, that while im typing it should show display the 开发者_如何学Clive text underneath.

its a simple script, but i cnt seem to see what the problem is, thanks :))

http://jsfiddle.net/XWsqz/


EDIT: Adding code from link.

$(document).ready(function(){
    $('#someTextBox').keyup(function(){
        $('#target').html(this.val());
    });
});

HTML

<textarea id="someTextBox"></textarea>
<div id="target"></div>


Should be $(this).val() instead of this.val() because this points to the DOM element and not the jquery element which has the .val() function defined:

$('#target').html($(this).val());


As everyone is suggesting, $(this).val() will work. However, it doesn't make a lot of sense to incur the overhead of creating a jQuery wrapped object on every single keypress. this.value is a better option if you don't need jQuery's extended methods on that element:

$(document).ready(function(){
  $('#someTextBox').keyup(function(){
    $('#target').html(this.value);
  });
});


Use $(this).val() instead of this.val().

The former is a jQuery object, the latter, I think, is simply a DOM node.

So:

$(document).ready(function(){
        $('#someTextBox').keyup(function(){
            $('#target').html($(this).val());
        });
    });

JS Fiddle demo.


You need to do $(this).val() instead of this.val()


.val() is a jQuery function, so it only works when you're calling a jQuery object.

this

is not jQuery on its own (in your example). You have to wrap it in $( ), like so:

$(this)

So the correct way would be like so:

$("#target").html( $(this).val() );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜