开发者

Why doesn't this javascript code work?

http://jsfiddle.net/FZQuM/2/

I want the div 'divi' to show what is in the input box 'hitbox'.

EDIT: it's really not difficult to place this code here, especially if it's this short

docum开发者_JAVA百科ent.getElementById('hitbox')
    .onchange(document.getElementById('divi')
               .innerHTML = document.getElementById('hitbox').value;


The native onchange event doesn't work like its jQuery counterpart.

Use

document.getElementById('hitbox').onchange = function() { ..... }


The onchange will fire only after the textbox lose focus - to see it "live" use something like onkeyup event:

document.getElementById('hitbox').onkeyup = function() {
    document.getElementById('divi').innerHTML =this.value;
};

(You can also use this while in the event handler)

Test case: http://jsfiddle.net/FZQuM/10/


Use .value instead of .text

And i made it an anonymous function:

document.getElementById('hitbox').onchange = function () {document.getElementById('divi').innerHTML = this.value};


Use "value" property instead of text. And use onchange event as property and not as onchange()

http://jsfiddle.net/FZQuM/3

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜